Windows中的短文件名与长文件名

时间:2010-06-01 20:10:44

标签: c++ windows file-io filesystems

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

原始文件的格式为

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

然而,在转换为short,然后返回long,文件名变为

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

1 个答案:

答案 0 :(得分:1)

反之亦然:GetShortPathNameA和GetLongPathNameW。

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

相关问题