我需要知道哪条是给定路径的真正路径.
例如:
真正的路径是:d:\ src\File.txt
用户给我:D:\ src\file.txt
我需要的结果是:d:\ src\File.txt
Bor*_*rja 17
你可以使用这个功能:
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern uint GetLongPathName(string ShortPath, StringBuilder sb, int buffer);
[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string longpath, StringBuilder sb, int buffer);
protected static string GetWindowsPhysicalPath(string path)
{
StringBuilder builder = new StringBuilder(255);
// names with long extension can cause the short name to be actually larger than
// the long name.
GetShortPathName(path, builder, builder.Capacity);
path = builder.ToString();
uint result = GetLongPathName(path, builder, builder.Capacity);
if (result > 0 && result < builder.Capacity)
{
//Success retrieved long file name
builder[0] = char.ToLower(builder[0]);
return builder.ToString(0, (int)result);
}
if (result > 0)
{
//Need more capacity in the buffer
//specified in the result variable
builder = new StringBuilder((int)result);
result = GetLongPathName(path, builder, builder.Capacity);
builder[0] = char.ToLower(builder[0]);
return builder.ToString(0, (int)result);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
作为一个老朋友,我总是为此目的使用FindFirstFile..Net翻译是:
Directory.GetFiles(Path.GetDirectoryName(userSuppliedName), Path.GetFileName(userSuppliedName)).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
这只能获得路径文件名部分的正确大小,而不是整个路径.
JeffreyLWhitledge的评论提供了一个递归版本的链接,该版本可以(但不总是)解决完整路径.
替代解决方案
这是一个适合我使用区分大小写的路径在 Windows 和服务器之间移动文件的解决方案。它沿着目录树走下去并用GetFileSystemEntries(). 如果路径的一部分无效(UNC 或文件夹名称),则它仅更正该点之前的路径,然后使用原始路径来查找找不到的路径。无论如何,希望这能节省其他人在处理同样问题时的时间。
private string GetCaseSensitivePath(string path)
{
var root = Path.GetPathRoot(path);
try
{
foreach (var name in path.Substring(root.Length).Split(Path.DirectorySeparatorChar))
root = Directory.GetFileSystemEntries(root, name).First();
}
catch (Exception e)
{
// Log("Path not found: " + path);
root += path.Substring(root.Length);
}
return root;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8432 次 |
| 最近记录: |