简体   繁体   English

获取文件夹的根目录+1

[英]Get root directory of a folder +1

How can i get the root directory of a folder +1? 如何获取文件夹+1的根目录?

Example: Input: C:\\Level1\\Level2\\level3 output should be: 示例:输入: C:\\Level1\\Level2\\level3输出应为:

Level1

If input is Level1 output should be Level1 如果输入是Level1输出应为Level1

if input is C:\\ output should be empty string 如果输入是C:\\输出应为empty string

Is there is a .Net function handles this? 是否有.Net功能处理这个?

Directory.GetDirectoryRoot will always returns C:\\ Directory.GetDirectoryRoot将始终返回C:\\

You can use the Path -class + Substring + Split to remove the root and get the top-folder. 您可以使用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(); 

Another way is using the DirectoryInfo class and it's Parent property: 另一种方法是使用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;
}

However, i would prefer the "lightweight" string methods. 但是,我更喜欢“轻量级”字符串方法。

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];
}

Not sure whether this is the correct way but you do: 不确定这是否是正确的方法,但你这样做:

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

Not sure whether DirectoryInfo object can get this in a cleaner manner.. 不确定DirectoryInfo对象是否可以以更干净的方式获取它。

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

You could use DirectoryInfo and a while loop. 您可以使用DirectoryInfowhile循环。

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

One possible solution, but may not be the best, is to find the position of @"\\", and do some manual processing yourself. 一个可能的解决方案,但可能不是最好的,是找到@“\\”的位置,并自己做一些手动处理。 Below code is not fully tested, just snippet: 下面的代码没有经过全面测试,只是片段:

//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;

Of course, this only works if we are sure we want chop substrings after the 2nd '\\'. 当然,这只有在我们确定我们想要在第二个'\\'后面的chop子串时才有效。

You could loop up using the directory info class using the following structure by adding the code section below into a method 您可以使用以下结构使用目录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

Why not just use System.IO.Path to retrieve the name? 为什么不使用System.IO.Path来检索名称?

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

This returns Level 1 . 这将返回Level 1

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

This returns empty string. 这将返回空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM