您的位置:首页 > 其它

MFC文件、文件夹操作整理

2013-08-24 13:44 288 查看
1、
判断文件是否存在
a)
利用CFile类和CFileStatus类判断


[c-sharp]
view plaincopyprint?

CFileStatus filestatus;

if (CFile::GetStatus(_T("d://softist.txt"), filestatus))

AfxMessageBox(_T("文件存在"));

else

AfxMessageBox(_T("文件不存在"));

b)
利用CFileFind类判断

[c-sharp]
view plaincopyprint?

CFileFind filefind;

CString strPathname = _T("d://softist.txt");

if(filefind.FindFile(strPathname))

AfxMessageBox(_T("文件存在"));

else

AfxMessageBox(_T("文件不存在"));

CFileFind filefind;

CString strPathname = _T("d://softist.txt");

if(filefind.FindFile(strPathname))

AfxMessageBox(_T("文件存在"));

else

AfxMessageBox(_T("文件不存在"));


c)
利用API函数FindFirstFile判断,这个函数还可以判断文件属性,日期,大小等属性。


[c-sharp]
view plaincopyprint?

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。

[c-sharp]
view plaincopyprint?

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);

}

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、
设置文件属性


[c-sharp]
view plaincopyprint?

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

[c-sharp]
view plaincopyprint?

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);

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


[c-sharp]
view plaincopyprint?

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 文件移动准备,下次启动系统时执行移动作业。

⑶、
删除文件

[c-sharp]
view plaincopyprint?

API:

BOOL DeleteFile( LPCTSTR lpFileName );

如:DeleteFile (_T("d://softist.txt"));

MFC:

static void PASCAL CFile::Remove(LPCTSTR lpszFileName);

API:

BOOL DeleteFile( LPCTSTR lpFileName );

如:DeleteFile (_T("d://softist.txt"));

MFC:

static void PASCAL CFile::Remove(LPCTSTR lpszFileName);


⑷、
文件更名MFC


[c-sharp]
view plaincopyprint?

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、
文件目录操作
⑴、
创建目录:

[c-sharp]
view plaincopyprint?

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 CreateDirectory(LPCTSTR pstrDirName);//pstrDirName是全路径

SECURITY_ATTRIBUTES  attribute;

attribute.nLength = sizeof(attribute);

attribute.lpSecurityDescriptor = NULL;

attribute.bInheritHandle = FALSE;

//创建

if(CreateDirectory("d://NewFolder",&attribute) == 0)

AfxMessageBox("创建失败!");


⑵、
删除目录:


[c-sharp]
view plaincopyprint?

BOOL RemoveDirectory( LPCTSTR lpPathName );

⑶、
判断目录是否存在(Shell Function)

[c-sharp]
view plaincopyprint?

#include <shlwapi.h>

#pragma comment(lib, "shlwapi.lib")

...

if (PathIsDirectory(_T("d://temp")))

AfxMessageBox(_T("存在"));

else

AfxMessageBox(_T("不存在"));

#include <shlwapi.h>

#pragma comment(lib, "shlwapi.lib")

...

if (PathIsDirectory(_T("d://temp")))

AfxMessageBox(_T("存在"));

else

AfxMessageBox(_T("不存在"));


⑷、
取得当前目录(API)


[c-sharp]
view plaincopyprint?

DWORD GetCurrentDirectory( DWORD nBufferLength, LPTSTR lpBuffer );

⑸、
取得执行文件所在目录(API)

[c-sharp]
view plaincopyprint?

DWORD GetModuleFileName( HMODULE hModule, LPTSTR lpFilename, DWORD nSize );

DWORD GetModuleFileName( HMODULE hModule, LPTSTR lpFilename, DWORD nSize );


⑹、
取得功能目录(Shell Function)


[c-sharp]
view plaincopyprint?

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);

}

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可以打出选择目录用的对话框界面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: