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 檔案移動準備,下次啟動系統時執行移動作業。
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可以打出選擇目錄用的對話框界面