我看到一个根据相对路径获取绝对路径的例子,但是不好使,请问各位Path.GetFullPath()的问题。
MSDN解释如下,但是我感觉到有问题。
string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'", path1, fullPath);

fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'", fileName, fullPath);

fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'", path2, fullPath);

// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'

用该方法并不能获取绝对的路径。
比如:应用程序的路径为:“E:\CurrentWork\ProtocolTest\ProtocolTest\obj\Debug”。
我以其中的“ProtocolTest\”为参数获取绝对路径但获取不到正确的结果,如果以“\ProtocolTest”为参数则会返回“E:\ProtocolTest”,反正是获取不到正确的结果。
请问各位这是怎么回事?
MSDN解释如下,但是我感觉到有问题。
string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;
fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'", path1, fullPath);
fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'", fileName, fullPath);
fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'", path2, fullPath);
// Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'

用该方法并不能获取绝对的路径。
比如:应用程序的路径为:“E:\CurrentWork\ProtocolTest\ProtocolTest\obj\Debug”。
我以其中的“ProtocolTest\”为参数获取绝对路径但获取不到正确的结果,如果以“\ProtocolTest”为参数则会返回“E:\ProtocolTest”,反正是获取不到正确的结果。
请问各位这是怎么回事?
浙公网安备 33010602011771号