2013-02-28 110 views
0

給定一個路徑和某個部分,我如何才能找到該部分下面的文件夾的名稱?如何在路徑的給定部分下找到文件夾?

這很難解釋,讓我舉一些例子。假設我正在查找'Dev/Branches'下的文件夾名稱。下面是例子投入,用粗體

  • C中的預計業績:\代碼\開發\分行\ 最新 \ BIN \ abc.dll
  • C:\開發\分行\ 5.1
  • d:\我的文檔

我正在使用C#\分行\ 7.0 \來源\測試\ test.cs中

編輯:我想我可以使用正則表達式/Dev/Branches/(.*?)/捕獲第一組,但有沒有正則表達式整潔的解決方案?無論如何,這個正則表達式在第二種情況下會失敗。

+0

你去將不得不m做出兩個假設之一。 「Branches」下方只有一個文件夾,或者您將按字母順序抓取第一個文件夾。這真的是你想要的嗎? – 2013-02-28 12:48:35

+1

@MichaelPerrenoud我相信OP意味着給定路徑中的下一部分 – 2013-02-28 12:49:34

+1

您不希望使用字符串查找函數來定位兩個斜線之間的字符串嗎? – roymustang86 2013-02-28 12:49:37

回答

0

我這個

public static string GetBranchName(string path, string prefix) 
{ 
    string folder = Path.GetDirectoryName(path); 

    // Walk up the path until it ends with Dev\Branches 
    while (!String.IsNullOrEmpty(folder) && folder.Contains(prefix)) 
    { 
     string parent = Path.GetDirectoryName(folder); 
     if (parent != null && parent.EndsWith(prefix)) 
      return Path.GetFileName(folder); 

     folder = parent; 
    } 

    return null; 
} 
0

把它分解成更小的步驟,就可以解決這個自己:

  1. (可選,取決於進一步的要求):獲取目錄名(文件是無關緊要的):Path.GetDirectoryName(string)
  2. 獲取它的父目錄Directory.GetParent(string)

這歸結爲;

var directory = Path.GetDirectoryName(input); 
var parentDirectory = Directory.GetParent(directory); 

提供的C:\Dev\Branches\5.1 - >5.1不符合您的規格,也就是輸入路徑本身的目錄名。這將輸出Branches

0
new Regex("\\?" + PathToMatchEscaped + "\\(\w+)\\?").Match()... 
1
// starting path 
string path = @"C:\Code\Dev\Branches\Latest\bin\abc.dll"; 

// search path 
string search = @"Dev\Branches"; 

// find the index of the search criteria 
int idx = path.IndexOf(search); 

// determine whether to exit or not 
if (idx == -1 || idx + search.Length >= path.Length) return; 

// get the substring AFTER the search criteria, split it and take the first item 
string found = path.Substring(idx + search.Length).Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); 

Console.WriteLine(found); 
1

這裏是一個會做完全相同的代碼,你期待什麼:

public static string GetSubdirectoryFromPath(string path, string parentDirectory, bool ignoreCase = true) 
{ 
    // 1. Standarize the path separators. 
    string safePath = path.Replace("/", @"\"); 
    string safeParentDirectory = parentDirectory.Replace("/", @"\").TrimEnd('\\'); 

    // 2. Prepare parentDirectory to use in Regex. 
    string directory = Regex.Escape(safeParentDirectory); 

    // 3. Find the immediate subdirectory to parentDirectory. 
    Regex match = new Regex(@"(?:|.+)" + directory + @"\\([^\\]+)(?:|.+)", ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); 

    // 4. Return the match. If not found, it returns null. 
    string subDirectory = match.Match(safePath).Groups[1].ToString(); 
    return subDirectory == "" ? null : subDirectory; 
} 

的測試代碼:

void Test() 
{ 
    string path1 = @"C:\Code\Dev\Branches\Latest\bin\abc.dll"; 
    string path2 = @"C:\Dev\Branches\5.1"; 
    string path3 = @"D:\My Documents\Branches\7.0\Source\test.cs"; 

    Console.WriteLine("Matches:"); 
    Console.WriteLine(GetSubdirectoryFromPath(path1, "dev/branches/") ?? "Not found"); 
    Console.WriteLine(GetSubdirectoryFromPath(path1, @"Dev\Branches") ?? "Not found"); 
    Console.WriteLine(GetSubdirectoryFromPath(path3, "D:/My Documents/Branches") ?? "Not found"); 
    // Incorrect parent directory. 
    Console.WriteLine(GetSubdirectoryFromPath(path2, "My Documents") ?? "Not found"); 
    // Case sensitive checks. 
    Console.WriteLine(GetSubdirectoryFromPath(path3, @"My Documents\Branches", false) ?? "Not found"); 
    Console.WriteLine(GetSubdirectoryFromPath(path3, @"my Documents\Branches", false) ?? "Not found"); 

    // Output: 
    // 
    // Matches: 
    // Latest 
    // Latest 
    // 7.0 
    // Not found 
    // 7.0 
    // Not found 
} 
相關問題