判斷文件和文件夾存在是幾種方法

一、判斷文件夾是否存在:

1. _access, _waccess 

                   http://msdn.microsoft.com/zh-tw/library/vstudio/1w06ktdy(v=vs.90).aspx

                2. CFileFind  遍歷目錄判斷是否存在

     http://msdn.microsoft.com/en-US/library/scx99850(v=vs.80).aspx

3.  PathIsDirectory 

             http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx

二、判斷文件是否存在:

              1. _access, _waccess 

                   http://msdn.microsoft.com/zh-tw/library/vstudio/1w06ktdy(v=vs.90).aspx

                2. CFileFind  遍歷目錄判斷是否存在

     http://msdn.microsoft.com/en-US/library/scx99850(v=vs.80).aspx

  1.      3. 使用CreateFile函數,函數原型爲:                                           HANDLE CreateFile(
      LPCTSTR
     lpFileName,          // pointer to name of the file
      DWORD dwDesiredAccess,       // access (read-write) mode
      DWORDdwShareMode,           // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                  
     // pointer to security attributes
      DWORD dwCreationDisposition // how to create
      DWORD dwFlagsAndAttributes // file attributes
      HANDLE hTemplateFile         // handle to file with attributes to
                                   // copy
    );

      LPCTSTR lpFileName,          // pointer to name of the file
      DWORD dwDesiredAccess,       // access (read-write) mode
      DWORDdwShareMode,           // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                  
     // pointer to security attributes
      DWORD dwCreationDisposition // how to create
      DWORD dwFlagsAndAttributes // file attributes
      HANDLE hTemplateFile         // handle to file with attributes to
                                   // copy
    );
  2. 4. 使用FindFirstFile函數,函數原型爲:                                 HANDLE FindFirstFile(
      LPCTSTR
     lpFileName // pointer to name of file to search for
      LPWIN32_FIND_DATA lpFindFileData
                           // pointer to returned information
    );

  3. 5. 使用GetFileAttributes函數,函數原型如下:                        DWORD GetFileAttributes(
      LPCTSTR
     lpFileName   // pointer to the name of a file or directory
    );

  4. 6. 使用Shell Lightweight Utility APIs函數
    PathFileExists()專門判斷文件和目錄時否存在的函數
    文件名可讀性比較強
    還可以判斷目錄是否存在
     
    Header: Declared in Shlwapi.h
    Import Library: Shlwapi.lib

 以上的各種方法供參考,函數具體用法需參見MSDN。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章