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

发布于 2024-09-03 14:26:21 字数 1463 浏览 3 评论 0原文

我有一些代码使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-09-10 14:26:21

反之亦然: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文