2011-10-27 66 views

回答

8

工程與正向和基於你問的問題反斜槓

static string GetRootFolder(string path) 
{ 
    while (true) 
    { 
     string temp = Path.GetDirectoryName(path); 
     if (String.IsNullOrEmpty(temp)) 
      break; 
     path = temp; 
    } 
    return path; 
} 
+0

噢,那真是太棒了。 – bobbymcr

+2

不要以爲會按照要求返回第一個目錄。但是,既然它已被標記爲答案,似乎它達到了目的......在這種情況下,問題是不正確的。請更新 – aateeque

+0

只要第一個目錄不以任何類型的斜線開始,否則它只會返回斜線。對於我在網址中的使用案例,我只是修剪了起始斜線。 –

6

好像你可以只使用弦上string.Split()方法,然後抓住第一個元素。
例子(未測試):

string str = "foo\bar\abc.txt"; 
string str2 = "bar/foo/foobar"; 


string[] items = str.split(new char[] {'/', '\'}, StringSplitOptions.RemoveEmptyEntries); 

Console.WriteLine(items[0]); // prints "foo" 

items = str2.split(new char[] {'/', '\'}, StringSplitOptions.RemoveEmptyEntries); 
Console.WriteLine(items[0]); // prints "bar" 
5

最健壯的解決方案將是使用DirectoryInfoFileInfo。在基於Windows NT的系統上,它應該接受分隔符的正向或反向。

using System; 
using System.IO; 

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     Console.WriteLine(GetTopRelativeFolderName(@"foo\bar\abc.txt")); // prints 'foo' 
     Console.WriteLine(GetTopRelativeFolderName("bar/foo/foobar")); // prints 'bar' 
     Console.WriteLine(GetTopRelativeFolderName("C:/full/rooted/path")); // ** throws 
    } 

    private static string GetTopRelativeFolderName(string relativePath) 
    { 
     if (Path.IsPathRooted(relativePath)) 
     { 
      throw new ArgumentException("Path is not relative.", "relativePath"); 
     } 

     FileInfo fileInfo = new FileInfo(relativePath); 
     DirectoryInfo workingDirectoryInfo = new DirectoryInfo("."); 
     string topRelativeFolderName = string.Empty; 
     DirectoryInfo current = fileInfo.Directory; 
     bool found = false; 
     while (!found) 
     { 
      if (current.FullName == workingDirectoryInfo.FullName) 
      { 
       found = true; 
      } 
      else 
      { 
       topRelativeFolderName = current.Name; 
       current = current.Parent; 
      } 
     } 

     return topRelativeFolderName; 
    } 
} 
0

這裏是萬一另一個例子,如果以下格式的路徑:

string path = "c:\foo\bar\abc.txt"; // or c:/foo/bar/abc.txt 
string root = Path.GetPathRoot(path); // root == c:\ 
+0

有_any_使用'c:/ foo/bar/abc.txt'約定的操作系統? –

+0

您需要轉義反斜槓或使用逐字字符串('@「...」')。 – bobbymcr

+0

轉義是必要的,我同意,否則字符串無效。無論慣例如何,示例都針對用戶的問題,如果提供了「c:/foo/bar/abc.txt」,代碼仍然有效。 – Dan

1

,以下應工作:

public string GetTopLevelDir(string filePath) 
    { 
     string temp = Path.GetDirectoryName(filePath); 
     if(temp.Contains("\\")) 
     { 
      temp = temp.Substring(0, temp.IndexOf("\\")); 
     } 
     else if (temp.Contains("//")) 
     { 
      temp = temp.Substring(0, temp.IndexOf("\\")); 
     } 
     return temp; 
    } 

當傳遞foo\bar\abc.txtfoo作爲wanted-同爲/箱

1

基於由Hasan Khan提供的答案...

private static string GetRootFolder(string path) 
{ 
    var root = Path.GetPathRoot(path); 
    while (true) 
    { 
     var temp = Path.GetDirectoryName(path); 
     if (temp != null && temp.Equals(root)) 
      break; 
     path = temp; 
    } 
    return path; 
} 

這將使最高級別文件夾

+0

這是你的答案。使用此方法打開: foo \ bar \ abc.txt -----> foo bar/foo/foobar -----> bar – Tyrant