gpt4 book ai didi

c# - 如何在 Windows 上获取区分大小写的路径?

转载 作者:IT王子 更新时间:2023-10-29 04:11:27 24 4
gpt4 key购买 nike

我需要知道哪个是给定路径的真实路径。

例如:

真实路径是:d:\src\File.txt
用户给我:D:\src\file.txt
结果我需要:d:\src\File.txt

最佳答案

你可以使用这个函数:

[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;
}

关于c# - 如何在 Windows 上获取区分大小写的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4763117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com