[英]Path.Combine absolute with relative path strings
I'm trying to join a Windows path with a relative path using Path.Combine
.我正在尝试使用
Path.Combine
将 Windows 路径与相对路径连接起来。
However, Path.Combine(@"C:\blah",@"..\bling")
returns C:\blah\..\bling
instead of C:\bling\
.但是,
Path.Combine(@"C:\blah",@"..\bling")
返回C:\blah\..\bling
而不是C:\bling\
。
Does anyone know how to accomplish this without writing my own relative path resolver (which shouldn't be too hard)?有谁知道如何在不编写我自己的相对路径解析器的情况下完成此操作(应该不会太难)?
What Works:什么有效:
string relativePath = "..\\bling.txt";
string baseDirectory = "C:\\blah\\";
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);
(result: absolutePath="C:\bling.txt") (结果:absolutePath="C:\bling.txt")
What doesn't work什么不起作用
string relativePath = "..\\bling.txt";
Uri baseAbsoluteUri = new Uri("C:\\blah\\");
string absolutePath = new Uri(baseAbsoluteUri, relativePath).AbsolutePath;
(result: absolutePath="C:/blah/bling.txt") (结果:absolutePath="C:/blah/bling.txt")
Call Path.GetFullPath on the combined path http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx在组合路径http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx 上调用 Path.GetFullPath
> Path.GetFullPath(Path.Combine(@"C:\blah\",@"..\bling"))
C:\bling
(I agree Path.Combine ought to do this by itself) (我同意 Path.Combine 应该自己做)
Path.GetFullPath(@"c:\windows\temp\..\system32")?
For windows universal apps Path.GetFullPath()
is not available, you can use the System.Uri
class instead:对于 windows 通用应用
Path.GetFullPath()
不可用,您可以使用System.Uri
class 代替:
Uri uri = new Uri(Path.Combine(@"C:\blah\",@"..\bling"));
Console.WriteLine(uri.LocalPath);
This will give you exactly what you need (path does NOT have to exist for this to work)这将为您提供您所需要的(路径不必存在才能工作)
DirectoryInfo di = new DirectoryInfo(@"C:\blah\..\bling");
string cleanPath = di.FullName;
Path.GetFullPath()
does not work with relative paths. Path.GetFullPath()
不适用于相对路径。
Here's the solution that works with both relative + absolute paths.这是适用于相对路径和绝对路径的解决方案。 It works on both Linux + Windows and it keeps the
..
as expected in the beginning of the text (at rest they will be normalized).它适用于 Linux + Windows 并且它保留
..
正如文本开头预期的那样(在 rest 处它们将被标准化)。 The solution still relies on Path.GetFullPath
to do the fix with a small workaround.该解决方案仍然依赖于
Path.GetFullPath
通过一个小的解决方法来进行修复。
It's an extension method so use it like text.Canonicalize()
这是一个扩展方法,所以像
text.Canonicalize()
一样使用它
/// <summary>
/// Fixes "../.." etc
/// </summary>
public static string Canonicalize(this string path)
{
if (path.IsAbsolutePath())
return Path.GetFullPath(path);
var fakeRoot = Environment.CurrentDirectory; // Gives us a cross platform full path
var combined = Path.Combine(fakeRoot, path);
combined = Path.GetFullPath(combined);
return combined.RelativeTo(fakeRoot);
}
private static bool IsAbsolutePath(this string path)
{
if (path == null) throw new ArgumentNullException(nameof(path));
return
Path.IsPathRooted(path)
&& !Path.GetPathRoot(path).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)
&& !Path.GetPathRoot(path).Equals(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal);
}
private static string RelativeTo(this string filespec, string folder)
{
var pathUri = new Uri(filespec);
// Folders must end in a slash
if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString())) folder += Path.DirectorySeparatorChar;
var folderUri = new Uri(folder);
return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString()
.Replace('/', Path.DirectorySeparatorChar));
}
Be careful with Backslashes, don't forget them (neither use twice:)小心反斜杠,不要忘记它们(不要使用两次:)
string relativePath = "..\\bling.txt";
string baseDirectory = "C:\\blah\\";
//OR:
//string relativePath = "\\..\\bling.txt";
//string baseDirectory = "C:\\blah";
//THEN
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.