1、
判断文件是否存在
a) 利用CFile类和CFileStatus类判断
- CFileStatus filestatus;
- if (CFile::GetStatus(_T("d://softist.txt"), filestatus))
- AfxMessageBox(_T("文件存在"));
- else
- AfxMessageBox(_T("文件不存在"));
b) 利用CFileFind类判断
- CFileFind filefind;
- CString strPathname = _T("d://softist.txt");
- if(filefind.FindFile(strPathname))
- AfxMessageBox(_T("文件存在"));
- else
- AfxMessageBox(_T("文件不存在"));
c) 利用API函数FindFirstFile判断,这个函数还可以判断文件属性,日期,大小等属性。
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind;
- hFind = FindFirstFile(_T("d://softist.txt"), &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox(_T("文件不存在"));
- }
- else
- {
- AfxMessageBox(_T("文件存在"));
- FindClose(hFind);
- }
2、 文件日期操作。下面是取得"d://softist.txt"的文件修改时间,TRACE以后,再把文件修改时间改成 2000-12-03 12:34:56。
- HANDLE hFile;
- FILETIME filetime;
- FILETIME localtime;
- SYSTEMTIME systemtime;
- hFile = CreateFile(_T("d://softist.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile != INVALID_HANDLE_VALUE)
- {
- GetFileTime(hFile, NULL, NULL, &filetime); //取得UTC文件时间
- FileTimeToLocalFileTime(&filetime, &localtime); //换成本地时间
- FileTimeToSystemTime(&localtime, &systemtime); //换成系统时间格式
- TRACE("%04d-%02d-%02d %02d:%02d:%02d/r/n",
- systemtime.wYear, systemtime.wMonth, systemtime.wDay,
- systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
- //把文件时间修改成 2000-12-03 12:34:56
- systemtime.wYear = 2000; systemtime.wMonth = 12; systemtime.wDay = 3;
- systemtime.wHour = 12; systemtime.wMinute = 34; systemtime.wSecond = 56;
- SystemTimeToFileTime(&systemtime, &localtime); //换成文件时间格式
- LocalFileTimeToFileTime(&localtime, &filetime); //换成UTC时间
- SetFileTime(hFile, NULL, NULL, &filetime); //设定UTC文件时间
- CloseHandle(hFile);
- }
3、 设置文件属性
- BOOL SetFileAttributes( LPCTSTR lpFileName, DWORD dwFileAttributes );
- dwFileAttributes 的意义:
- FILE_ATTRIBUTE_ARCHIVE 保存文件
- FILE_ATTRIBUTE_HIDDEN 隐藏文件
- FILE_ATTRIBUTE_NORMAL 通常文件
- FILE_ATTRIBUTE_READONLY 只读文件
- FILE_ATTRIBUTE_SYSTEM 系统文件
- 例:
- SetFileAttributes(_T("d://softist.txt", FILE_ATTRIBUTE_READONLY);
4、 文件的复制,移动,删除,更名
⑴、 文件的复制API
- BOOL CopyFile(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists);
- bFailIfExists用来制定如果目标文件已经存在时,是否中止复制操作,返回FALSE。例,把"d://softist1.txt"复制到"d://softist2.txt",即使"d://softist2.txt"已经存在。
- BOOL bRet = CopyFile(_T("d://softist1.txt"), _T("d://softist2.txt"), FALSE);
⑵、 文件的移动API
- BOOL MoveFile(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName );
- 这个函数可以移一个文件,或目录(包括子目录),例:
- MoveFile(_T("d://softist.txt"), _T("e//softist2.txt"));//移动并改名
- 下面的API带着选项dwFlags ,移动文件,或目录(包括子目录)。
- BOOL MoveFileEx(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, DWORD dwFlags );
- dwFlags的意义:
- MOVEFILE_REPLACE_EXISTING 如果目标文件存在是否替代它 。
- MOVEFILE_DELAY_UNTIL_REBOOT 文件移动准备,下次启动系统时执行移动作业。
⑶、 删除文件
- API:
- BOOL DeleteFile( LPCTSTR lpFileName );
- 如:DeleteFile (_T("d://softist.txt"));
- MFC:
- static void PASCAL CFile::Remove(LPCTSTR lpszFileName);
⑷、 文件更名MFC
- TCHAR* pOldName = _T("Oldname_File.dat");
- TCHAR* pNewName = _T("Renamed_File.dat");
- try
- {
- CFile::Rename(pOldName, pNewName);
- }
- catch(CFileException* pEx )
- {
- TRACE(_T("File %20s not found, cause = %d/n"), pOldName, pEx->m_cause);
- pEx->Delete();
- }
5、 文件目录操作
⑴、 创建目录:
- BOOL CreateDirectory(LPCTSTR pstrDirName);//pstrDirName是全路径
- SECURITY_ATTRIBUTES attribute;
- attribute.nLength = sizeof(attribute);
- attribute.lpSecurityDescriptor = NULL;
- attribute.bInheritHandle = FALSE;
- //创建
- if(CreateDirectory("d://NewFolder",&attribute) == 0)
- AfxMessageBox("创建失败!");
⑵、 删除目录:
- BOOL RemoveDirectory( LPCTSTR lpPathName );
⑶、 判断目录是否存在(Shell Function)
- #include <shlwapi.h>
- #pragma comment(lib, "shlwapi.lib")
- ...
- if (PathIsDirectory(_T("d://temp")))
- AfxMessageBox(_T("存在"));
- else
- AfxMessageBox(_T("不存在"));
⑷、 取得当前目录(API)
- DWORD GetCurrentDirectory( DWORD nBufferLength, LPTSTR lpBuffer );
⑸、 取得执行文件所在目录(API)
- DWORD GetModuleFileName( HMODULE hModule, LPTSTR lpFilename, DWORD nSize );
⑹、 取得功能目录(Shell Function)
- BOOL SHGetSpecialFolderPath( HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate);
- 例:读取我的档案目录
- TCHAR szDirFile[1024];
- memset(szDirFile, 0, sizeof(szDirFile));
- BOOL bRet = SHGetSpecialFolderPath(NULL,szDirFile,CSIDL_PERSONAL,true);
- if (bRet)
- {
- AfxMessageBox(szDirFile);
- }
⑺、 选择目录用的对话框界面
利用Shell Function可以打出选择目录用的对话框界面

本文详细介绍了使用C/C++进行文件及目录的操作方法,包括文件的判断、日期操作、属性设置、复制移动删除及重命名等功能,同时涵盖了目录的创建、删除、判断存在性等实用技巧。
2398

被折叠的 条评论
为什么被折叠?



