vc 檔案存在 檔案屬性 刪除檔案

來源:互聯網
上載者:User

一:隱藏檔案。

  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中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.