【发布时间】:2017-06-19 02:44:40
【问题描述】:
我想检查指定位置是否存在某个文件。我为此尝试了多种解决方案,但似乎没有一个能正常工作,因为它们都返回 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
是不是我做错了什么?
【问题讨论】:
-
您至少没有收到编译器警告吗?比如
warning C4129: 'W' : unrecognized character escape sequence? -
@MichaelWalz 我做到了。然而双斜线使警告消失。
-
警告通常是错误的。
标签: c++ visual-studio c++11 winapi visual-studio-2012