我想检查指定位置是否存在某个文件。我一直在为此尝试多种解决方案,但似乎没有一个能正常工作,因为它们都返回 false。
毫无疑问,该文件存在于指定位置。
可执行文件正在以管理员身份运行,因此我拥有适当的权限。
我使用的代码:
#include <io.h>
#include <string>
#include <Shlwapi.h>
std::string str = "C:\WINDOWS\System32\iluminated.dll";
unsigned long attrib = GetFileAttributes(str.c_str());
bool exists1 = (attrib != INVALID_FILE_ATTRIBUTES &&
!(attrib & FILE_ATTRIBUTE_DIRECTORY)) &&
GetLastError() != ERROR_FILE_NOT_FOUND; // false
bool exists2 = ( _access( str.c_str(), 0 ) != -1 ); // false
bool exists3 = PathFileExists(str.c_str()) != 0; // false
我做错了什么吗?
最佳答案
路径应使用双反斜杠,因为如果在字符串中使用单反斜杠,它们将被解释为命令符号(例如 \n
行):
"C:\\WINDOWS\\System32\\iluminated.dll"
或者,您可以使用正斜杠,它们适用于大多数操作系统:
"C:/WINDOWS/System32/iluminated.dll"
关于c++ - WINAPI:文件存在检查失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995269/