因为Path.Combine
并非在所有情况下都有效,所以这里有一个更复杂的功能:-)
static string GetFullPath(string maybeRelativePath, string baseDirectory) {
if (baseDirectory == null) baseDirectory = Environment.CurrentDirectory;
var root = Path.GetPathRoot(maybeRelativePath);
if (string.IsNullOrEmpty(root))
return Path.GetFullPath(Path.Combine(baseDirectory, maybeRelativePath));
if (root == "\\")
return Path.GetFullPath(Path.Combine(Path.GetPathRoot(baseDirectory), maybeRelativePath.Remove(0, 1)));
return maybeRelativePath;
}
Path.Combine(@"C:\foo\",@"\foo\bar")
返回@"\foo\bar"
而不是预期@"C:\foo\bar"