Windows 中的短文件名与长文件名
我有一些代码使用 GetShortNameW() 从文件路径获取短名称,然后检索长名称视图 GetLongNameA()。
原始文件的形式为
"C:/ProgramData/My Folder/File.ext"
但是,在转换为短名称,然后返回长名称后,文件名变为
"C:/Program Files/My Folder/Filename.ext".
短名称的形式
"C:/PROGRA~2/MY_FOL~1/FIL~1.EXT"
短名称被错误解析。
该代码在 Windows 7 上使用 VS 2005 进行编译(我无法将项目升级到 VS2008)
有人知道为什么会发生这种情况吗?
DWORD pathLengthNeeded = ::GetShortPathNameW(aRef->GetFilePath().c_str(), NULL, 0);
if(pathLengthNeeded != 0)
{
WCHAR* shortPath = new WCHAR[pathLengthNeeded];
DWORD newPathNameLength = ::GetShortPathNameW(aRef->GetFilePath().c_str(), shortPath, pathLengthNeeded);
if(newPathNameLength != 0)
{
UI_STRING unicodePath(shortPath);
std::string asciiPath = StringFromUserString(unicodePath);
pathLengthNeeded = ::GetLongPathNameA(asciiPath.c_str(),NULL, 0);
if(pathLengthNeeded != 0)
{// convert it back to a long path if possible. For goodness sake can't we use Unicode throughout?F
char* longPath = new char[pathLengthNeeded];
DWORD newPathNameLength = ::GetLongPathNameA(asciiPath.c_str(), longPath, pathLengthNeeded);
if(newPathNameLength != 0)
{
std::string longPathString(longPath, newPathNameLength);
asciiPath = longPathString;
}
delete [] longPath;
}
SetFullPathName(asciiPath);
}
delete [] shortPath;
}
I have some code which gets the short name from a file path, using GetShortNameW(), and then later retrieves the long name view GetLongNameA().
The original file is of the form
"C:/ProgramData/My Folder/File.ext"
However, following conversion to short, then back to long, the filename becomes
"C:/Program Files/My Folder/Filename.ext".
The short name is of the form
"C:/PROGRA~2/MY_FOL~1/FIL~1.EXT"
The short name is being incorrectly resolved.
The code compiles using VS 2005 on Windows 7 (I cannot upgrade the project to VS2008)
Does anybody have any idea why this might be happening?
DWORD pathLengthNeeded = ::GetShortPathNameW(aRef->GetFilePath().c_str(), NULL, 0);
if(pathLengthNeeded != 0)
{
WCHAR* shortPath = new WCHAR[pathLengthNeeded];
DWORD newPathNameLength = ::GetShortPathNameW(aRef->GetFilePath().c_str(), shortPath, pathLengthNeeded);
if(newPathNameLength != 0)
{
UI_STRING unicodePath(shortPath);
std::string asciiPath = StringFromUserString(unicodePath);
pathLengthNeeded = ::GetLongPathNameA(asciiPath.c_str(),NULL, 0);
if(pathLengthNeeded != 0)
{// convert it back to a long path if possible. For goodness sake can't we use Unicode throughout?F
char* longPath = new char[pathLengthNeeded];
DWORD newPathNameLength = ::GetLongPathNameA(asciiPath.c_str(), longPath, pathLengthNeeded);
if(newPathNameLength != 0)
{
std::string longPathString(longPath, newPathNameLength);
asciiPath = longPathString;
}
delete [] longPath;
}
SetFullPathName(asciiPath);
}
delete [] shortPath;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反之亦然:GetShortPathNameA 和 GetLongPathNameW。
GetLongPathNameA 不一定会成功。
只有 8.3 路径名[部分保证]与 ANSI 兼容。
It should have been vice versa: GetShortPathNameA and GetLongPathNameW.
GetLongPathNameA should not necessarily succeed.
Only 8.3 pathnames are [partially guaranteed] to be ANSI-compatible.