Kri*_*nan 67 windows winapi file
我们如何使用Win32程序检查文件是否存在?我正在为Windows Mobile App工作.
Zac*_*ame 197
使用GetFileAttributes检查文件系统对象存在,它不是一个目录.
BOOL FileExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
Run Code Online (Sandbox Code Playgroud)
cod*_*ict 34
您可以使用该功能GetFileAttributes.0xFFFFFFFF如果文件不存在,则返回.
Pre*_*gha 24
你可以打电话FindFirstFile.
这是我刚敲过的一个例子:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int fileExists(TCHAR * file)
{
WIN32_FIND_DATA FindFileData;
HANDLE handle = FindFirstFile(file, &FindFileData) ;
int found = handle != INVALID_HANDLE_VALUE;
if(found)
{
//FindClose(&handle); this will crash
FindClose(handle);
}
return found;
}
void _tmain(int argc, TCHAR *argv[])
{
if( argc != 2 )
{
_tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]);
return;
}
_tprintf (TEXT("Looking for file is %s\n"), argv[1]);
if (fileExists(argv[1]))
{
_tprintf (TEXT("File %s exists\n"), argv[1]);
}
else
{
_tprintf (TEXT("File %s doesn't exist\n"), argv[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
Pie*_*rre 16
怎么样简单:
#include <io.h>
if(_access(path, 0) == 0)
... // file exists
Run Code Online (Sandbox Code Playgroud)
另一种选择: 'PathFileExists'.
但我可能会去GetFileAttributes.