簡體   English   中英

獲取文件夾的根目錄+1

[英]Get root directory of a folder +1

如何獲取文件夾+1的根目錄?

示例:輸入: C:\\Level1\\Level2\\level3輸出應為:

Level1

如果輸入是Level1輸出應為Level1

如果輸入是C:\\輸出應為empty string

是否有.Net功能處理這個?

Directory.GetDirectoryRoot將始終返回C:\\

您可以使用Path -class + Substring + Split刪除根並獲取頂級文件夾。

// your directory:
string dir = @"C:\Level1\Level2\level3";     

// C:\  
string root = Path.GetPathRoot(dir); 

// Level1\Level2\level3:
string pathWithoutRoot = dir.Substring(root.Length);       

// Level1
string firstFolder = pathWithoutRoot.Split(Path.DirectorySeparatorChar).First(); 

另一種方法是使用DirectoryInfo類,它的Parent屬性:

DirectoryInfo directory = new DirectoryInfo(@"C:\Level1\Level2\level3");
string firstFolder = directory.Name;
while (directory.Parent != null && directory.Parent.Name != directory.Root.Name)
{
    firstFolder = directory.Parent.Name;
    directory = directory.Parent;
}

但是,我更喜歡“輕量級”字符串方法。

string dir = @"C:\foo\bar\woah";
var dirSegments = dir.Split(new char[] { Path.DirectorySeparatorChar }, 
                            StringSplitOptions.RemoveEmptyEntries);
if (dirSegments.Length == 1)
{
    return string.Empty;
}
else
{
    return dirSegments[0] + Path.DirectorySeparatorChar + dirSegments[1];
}

不確定這是否是正確的方法,但你這樣做:

string s = @"C:\Level1\Level2\Level3";
string foo = s.split(@"\")[1];

不確定DirectoryInfo對象是否可以以更干凈的方式獲取它。

DirectoryInfo di = new DirectoryInfo(@"C:\Level1\Level2\Level3");
di.Root;

您可以使用DirectoryInfowhile循環。

DirectoryInfo info = new DirectoryInfo(path);
while (info.Parent != null && info.Parent.Parent != null)
    info = info.Parent;
string result = info.FullName;

一個可能的解決方案,但可能不是最好的,是找到@“\\”的位置,並自己做一些手動處理。 下面的代碼沒有經過全面測試,只是片段:

//var positions = inputString.IndexOfAny(new [] {'\'});  //Original one
//Updated, thanks to Snixtor's implementation 
var positions = inputString.IndexOfAny(new [] {Path.DirectorySeparatorChar}); 
int n=positions.Length;
if(n>=1)
{
     var pos = positions[1];  //The 2nd '\';
     return inputString.SubString(0, pos);
}
return null;

當然,這只有在我們確定我們想要在第二個'\\'后面的chop子串時才有效。

您可以使用以下結構使用目錄info類循環,方法是將下面的代碼部分添加到方法中

string path = "C:\Level1\Level2\level3";
DirectoryInfo d = new DirectoryInfo(path);
while (d.Parent.FullName != Path.GetPathRoot(path))
{
    d = d.Parent;
}
return d.FullName;

一個快樂的linq one liner:

string level1_2 = Directory.GetDirectoryRoot(path) + path.Split(Path.DirectorySeparatorChar).Skip(1).Take(1).DefaultIfEmpty("").First();
  var di = new System.IO.DirectoryInfo(@"C:\a\b\c");
  Func<DirectoryInfo, DirectoryInfo> root = null;
  root = (info) => info.Parent.FullName.EndsWith("\\") ? info : root(info.Parent);
  var rootName = root(di).Name; //#a

為什么不使用System.IO.Path來檢索名稱?

MessageBox.Show(System.IO.Path.GetFileName(
    System.IO.Path.GetDirectoryName(
        System.IO.Path.GetDirectoryName(@"C:\Level1\Level2\Level3")
    )
));

這將返回Level 1

MessageBox.Show(System.IO.Path.GetFileName(
    System.IO.Path.GetDirectoryName(
        System.IO.Path.GetDirectoryName(@"C:\")
    )
));

這將返回空字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM