一:隱藏檔案。
1.WinExec函數。
該函數執行一個cmd命令,如修改
C://Documents and Settings//eMLab//Application Data//test.txt
檔案屬性為隱藏可以:
CString strFileName =
"C://Documents and Settings//eMLab//Application Data//test.txt";
CString strCmd = "attrib +h" + strFileName;
WinExec(strCmd,0);
attrib修改檔案屬性,+h表示給檔案加上隱藏屬性。
2.SetFileAttributes函數
原型:BOOL SetFileAttributes(LPCTSTR lpFileName, //file name
WORD dwFileAttributes //file attribute
);
如:
SetFileAttributes(strFileName,FILE_ATTRIBUTE_HIDDEN);
FILE_ATTRIBUTE_HIDDEN就表示隱藏屬性。
3.CFile和CFileStatus類
CFile的靜態函數GetStatus可以讀取檔案狀態
CFile的靜態函數SetStatus可以修改檔案狀態
如:
FileStatus fs;
CFile::GetStatus(strFileName,fs);
fs.m_attribute = CFile::hidden; //set hidden attribute
CFile::SetStatus(strFileName,fs);
二:判斷檔案是否存在。
1._access函數,在io.h中。
原型:int _access(const char *filename, int amode);
參數amode(好象有5種模式)
0:檢查檔案是否存在
1:檢查檔案是否可運行
2:檢查檔案是否可寫訪問
4:檢查檔案是否可讀訪問
還有一種,由於MSDN突然壞了,暫時保留著
if ( _access(file,0) )
{
//檔案不存在
}
2.CFile和CFileStatus類
CFile的靜態函數GetStatus如果返回FALSE表示檔案不存在
CFileStatus fs;
if ( !CFile::GetStatus(strFileName,fs) )
{
//檔案不存在
}
3.CFileFind類
直接使用該類的成員函數FindFile進行判斷
CFileFind ff;
if ( !ff.FindFile(strFileName) )
{
//檔案不存在
}
ff.Close();
3.判斷檔案夾是否存在
DirExists(sPath);
三:刪除檔案
CFile TempFile;
TempFile.Remove(指定檔案名稱);
////////////////////////////////////////////////////////
_rmdir(),刪除目錄
////////////////////////////////////////////////////////
DeleteFile()刪除檔案
////////////////////////////////////////////////////////
if(::DeleteFile("C://autoexec.bat"))
AfxMessageBox("刪除檔案成功");
DeleteFile定義在windows.h和Winbase.h中