1. 使用_access函数,函数原型为 int _access( const char *path, int mode );
2. 使用CreateFile函数,函数原型为: HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write)
mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDisposition, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes
to // copy );
3. 使用FindFirstFile函数,函数原型为: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData
// pointer to returned information );
//例子:
BOOL CPubFunc::DirectoryExist(CString Path)
{
WIN32_FIND_DATA fd;
BOOL ret = FALSE;
HANDLE hFind = FindFirstFile(Path, &fd);
if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{ //目录存在
ret = TRUE;
}
FindClose(hFind);
return ret;
}
4. 使用GetFileAttributes函数,函数原型如下: DWORD GetFileAttributes( LPCTSTR lpFileName // pointer to the name of a file or directory );
5.使用Shell Lightweight Utility APIs函数PathFileExists()专门判断文件和目录时否存在的函数文件名可读性比较强还可以判断目录是否存在
Header: Declared in Shlwapi.h Import Library: Shlwapi.lib 以上的各种方法供参考,函数具体用法需参见MSDN。
//这是MSDN中的例子:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Valid file path name (file is there).
char buffer_1[] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Invalid file path name (file is not there).
char buffer_2[] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// Return value from "PathFileExists".
int retval;
// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
else{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
}
6.使用CFileFind类....这是一个InternetServices类,在此可以借用一下。也可以用于遍历文件夹(s可指定深度)
BOOL CPubFunc::FileExist(CString FileName)
{
CFileFind fFind;
return fFind.FindFile(FileName);
}
//创建目录
#include <winbase.h>
BOOL CreateDirectory(
LPCTSTRlpPathName,//
pointer to directory path string
LPSECURITY_ATTRIBUTESlpSecurityAttributes
// pointer to security descriptor
);
分享到:
相关推荐
C语言判断文件夹或者文件是否存在的方法 C语言判断文件夹
VC中的文件操作,判断文件、文件夹是否存在 、删除文件夹、求文件夹大小.很详细
压缩包里面的东西是关于用vc判断一个文件是否存在的一些东西,大家可以看一下
VC 可视化判断文件是否存在,检查指定的文件是否存在,在窗口的输入框中输入文件路径和文件名,也就是完整的路径即可,点击“确定”按钮,即可检查文件。
本文实例讲述了VC判断一个文件为目录的方法,分享给大家供大家参考。具体实现方法如下: 这是一个自定义函数,用于判断一个文件是否为目录: 代码如下:/** * check whether a file is a directory @return ...
判断文件是否被改动vc源代码,完整的,已调试好
简单好用的VC代码,VC打开目录下的文件代码
VC判断文件是否被改动。
vc读取大文件的高效的方法
VC编写的选择目录下所有文件的源代码,有注释
VC 文件目录变化监控
1.Linux下文件句柄方式,写二进制文件时一定要用O_BINARY 2.CFile,MFC封装的形式,不区分二进制或者文本文件方式读写 3.FILE,文件指针形式,写二进制文件时一定要用wb
VC.net 过滤指定目录下的文件 VC.net 过滤指定目录下的文件
VC源码,以供学习与扩展有关监视文件/目录等是否修改等情况
vc 选择目录的方法 vc 选择目录的方法 vc 选择目录的方法
我收藏的参考资料 防止人为更改文件后缀名,逃避文件类型检查.
对vc下bmp文件显示具体方法的实现,有具体程序,为期刊论文,便于大家使用
派生一个按钮类,用于判断鼠标是否正处于按钮内,如果处于则设置按钮焦点
简单好用,解释全面的VC下选择文件目录代码