Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何确定相对路径的最顶层文件夹(根)?_C#_.net_Path - Fatal编程技术网

C# 如何确定相对路径的最顶层文件夹(根)?

C# 如何确定相对路径的最顶层文件夹(根)?,c#,.net,path,C#,.net,Path,给定相对路径字符串: "SomeFolder\\Container\\file.txt" 我想确定最顶层的父文件夹或根文件夹,“SomeFolder” 我希望避免使用“字符串巫毒”,这样就可以将其移植到具有不同目录分隔符的系统中 有什么明显的方法是我忽略的吗?来自: 返回路径的根目录,例如 “C:\”,如果路径为null,则为null;如果路径不为null,则为空字符串 包含根目录信息 你的道路没有根。这就是为什么会得到空字符串的结果。在调用GetPathRoot()之前,应该首先检查路径是否

给定相对路径字符串:

"SomeFolder\\Container\\file.txt"
我想确定最顶层的父文件夹或根文件夹,
“SomeFolder”

我希望避免使用“字符串巫毒”,这样就可以将其移植到具有不同目录分隔符的系统中

有什么明显的方法是我忽略的吗?

来自:

返回路径的根目录,例如 “C:\”,如果路径为null,则为null;如果路径不为null,则为空字符串 包含根目录信息

你的道路没有根。这就是为什么会得到空字符串的结果。在调用
GetPathRoot()
之前,应该首先检查路径是否为根路径


@威尔建议使用
Uri
类,我已经将它用于其他一些路径操作,效果非常好

我用以下方法解决了这个问题:

/// <summary>
/// Returns the path root if absolute, or the topmost folder if relative.
/// The original path is returned if no containers exist.
/// </summary>
public static string GetTopmostFolder(string path)
{
    if (path.StartsWith(Path.DirectorySeparatorChar.ToString()))
        path = path.Substring(1);

    Uri inputPath;

    if (Uri.TryCreate(path, UriKind.Absolute, out inputPath))
        return Path.GetPathRoot(path);

    if (Uri.TryCreate(path, UriKind.Relative, out inputPath))
        return path.Split(Path.DirectorySeparatorChar)[0];

    return path;
}
//
///如果是绝对路径,则返回路径根;如果是相对路径,则返回最上面的文件夹。
///如果不存在容器,则返回原始路径。
/// 
公共静态字符串GetTopmostFolder(字符串路径)
{
if(path.StartsWith(path.directorysepositionorchar.ToString())
路径=路径子字符串(1);
Uri输入路径;
if(Uri.TryCreate(path,UriKind.Absolute,out-inputPath))
返回Path.GetPathRoot(路径);
if(Uri.TryCreate(path,UriKind.Relative,out-inputPath))
返回path.Split(path.directoryseportorchar)[0];
返回路径;
}
编辑:


修改为去掉前导目录分隔符。我不希望有任何输入字符串,但最好是以防万一。

对于这种类型的方案,
Uri
类有很多方法,但是我不能说什么方法在所有情况下都有效。将协助移植字符串巫术方法,您可以将路径“规范化”以使用
/
而不是\,因为Windows操作系统将乐于接受这两种方法。投票人:您认为我可以做些什么来改进此问题?@Blorgbeard我的编辑回答了这个问题。如果路径不是
路径
帮助程序可以使用的形式,我会求助于字符串操作。然而,有趣的方法是,如果路径的形式是
\\SomeFolder\\Container\\file.txt
,它就会中断。这是一个很好的观点。我不希望这是一个输入,但我会用一种方法来删除引导目录分隔符的方法,根据需要,我会考虑可选的。听起来像“字符串Voodoo”<代码>=P<代码>,至少它是跨平台兼容的(我们希望)Vodoo。
var somePath = "SomeFolder\\Container\\file.txt";

String root;
if (Path.IsPathRooted(somePath))
    root = Path.GetPathRoot(somePath);
else
    root = somePath.Split(Path.DirectorySeparatorChar).First();
/// <summary>
/// Returns the path root if absolute, or the topmost folder if relative.
/// The original path is returned if no containers exist.
/// </summary>
public static string GetTopmostFolder(string path)
{
    if (path.StartsWith(Path.DirectorySeparatorChar.ToString()))
        path = path.Substring(1);

    Uri inputPath;

    if (Uri.TryCreate(path, UriKind.Absolute, out inputPath))
        return Path.GetPathRoot(path);

    if (Uri.TryCreate(path, UriKind.Relative, out inputPath))
        return path.Split(Path.DirectorySeparatorChar)[0];

    return path;
}