如何检查给定路径是否指向现有文件 OR 目录

how to check if given path is pointing to existing file OR directory?

本文关键字:文件 目录 OR 是否 路径 何检查 检查      更新时间:2023-10-16

我需要一个函数,如果给定路径上有一个实体,无论是文件还是目录,它都会简单地返回布尔值。在 winapi 或 stl 中使用什么函数?

GetFileAttributes()将返回有关文件系统对象的信息,可以查询这些信息以确定它是文件还是目录,如果不存在,它将失败。

例如:

#include <windows.h>
#include <iostream>
int main(int argc, char* argv[])
{
    if (2 == argc)
    {
        const DWORD attributes = GetFileAttributes(argv[1]);
        if (INVALID_FILE_ATTRIBUTES != attributes)
        {
            std::cout << argv[1] << " exists.n";
        }
        else if (ERROR_FILE_NOT_FOUND == GetLastError())
        {
            std::cerr << argv[1] << " does not existn";
        }
        else
        {
            std::cerr << "Failed to query "
                      << argv[1] 
                      << " : "
                      << GetLastError()
                      << "n";
        }
    }
    return 0;
}

There is PathFileExists (shlwapi)

确定文件系统对象(如文件或文件)的路径 文件夹有效。

(UNC 股份的注意事项)