c++ - 为什么 GetFullPathName 返回工作目录?

标签 c++ winapi path

我偶然发现了 GetFullPathName() 的行为(通过使用 QFileInfo::canonicalFilePath() )我不太明白:当我用一个由当前驱动器号和一个冒号组成的字符串调用这个函数时,它返回当前工作目录的路径,而我希望驱动器号的路径。

下面的代码举例说明了我在说什么:

#include <windows.h>
#include <iostream>
#include <string>

std::string canonicalFilePath(const char *path)
{
    static const std::size_t BufferSize = 300;
    char canonicalPath[BufferSize];

    GetFullPathName(path, BufferSize, canonicalPath, 0);

    return std::string(canonicalPath);
}

int main(int, char **)
{
    SetCurrentDirectory("C:/some/path");
    std::cout << "In C:" << '\n';
    std::cout << "  C   -> " << canonicalFilePath("C")   << '\n'
              << "  C:  -> " << canonicalFilePath("C:")  << '\n'
              << "  C:/ -> " << canonicalFilePath("C:/") << '\n'
              << "  D   -> " << canonicalFilePath("D")   << '\n'
              << "  D:  -> " << canonicalFilePath("D:")  << '\n'
              << "  D:/ -> " << canonicalFilePath("D:/") << '\n';

    SetCurrentDirectory("D:/other/path");
    std::cout << "In D:" << '\n';
    std::cout << "  C   -> " << canonicalFilePath("C")   << '\n'
              << "  C:  -> " << canonicalFilePath("C:")  << '\n'
              << "  C:/ -> " << canonicalFilePath("C:/") << '\n'
              << "  D   -> " << canonicalFilePath("D")   << '\n'
              << "  D:  -> " << canonicalFilePath("D:")  << '\n'
              << "  D:/ -> " << canonicalFilePath("D:/") << '\n';
}

输出:

In C:
  C   -> C:\some\path\C      // ok
  C:  -> C:\some\path        // ? why not C:\ ?
  C:/ -> C:\                 // ok
  D   -> C:\some\path\D      // ok
  D:  -> D:\                 // ok
  D:/ -> D:\                 // ok
In D:
  C   -> D:\other\path\C     // ok
  C:  -> C:\                 // ok
  C:/ -> C:\                 // ok
  D   -> D:\other\path\D     // ok
  D:  -> D:\other\path       // ? why not D:\ ?
  D:/ -> D:\                 // ok

这种行为正常吗?在 GetFullPathName documentation , 据称

If you specify "U:" the path returned is "U:\"

如果“U”是当前驱动器盘符,为什么不是这种情况?

最佳答案

来自 msdn :

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk.

Examples of this format are as follows:

  1. "C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C.
  2. "C:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.

关于c++ - 为什么 GetFullPathName 返回工作目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19677387/

相关文章:

c++ - 后处理窗口视频输出

c++ - 默认创建类 `final`还是给它们一个虚拟的析构函数?

java - 如何更正带注释的 websocket 类的路径?

perl - make_path 未按指定设置模式

objective-c - 意外的应用程序支持路径

c++ - 如何在 Visual Studio 2012 中创建和运行简单的 C++ 程序?

c++ - 在 ARM 上使用 Qt 的简单程序中的段错误

C++ NetBeans Win32 hwnd 图标

两个线程可以使用相同的线程过程吗?

c++ - 在 win32、WM_CHAR 或 WM_KEYDOWN/WM_KEYUP 中处理键盘输入?