2010-06-01 62 views
2

我有一些代码,它使用GetShortNameW()从文件路径中获取短名称,然后再检索长名称视图GetLongNameA()。短文件名与Windows中的长文件名

原始文件的形式是

"C:/ProgramData/My Folder/File.ext" 

然而,以下转换到短,然后再返回到长,文件名变为

"C:/Program Files/My Folder/Filename.ext". 

短名称的形式为

"C:/PROGRA~2/MY_FOL~1/FIL~1.EXT" 

短名称被错误地解决。

代码编译使用VS 2005的Windows 7(我不能升级的项目,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; 
    } 
+0

对于包含该文件的目录,“dir/x”的输出是什么? – 2010-06-01 20:14:25

+0

它与GetShortPathName给出的短名称一致 - C:\ ProgramData \ ...映射到C:\ PROGRA〜2 \ ...,并且C:\ Program Files \ ...映射到C:\ PROGRA〜 1 \ ... 通过GetLongPathName以编程方式将短名称转换回长名称似乎解决不正确。 – 2010-06-02 13:57:19

回答

1

它应该反之亦然:GetShortPathNameA和GetLongPathNameW。

GetLongPathNameA不一定会成功。 只有8.3路径名[部分保证]是ANSI兼容的。