您的位置:首页 > 其它

VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法

2013-04-22 13:52 946 查看
目录是否存在检查:

 
[cpp] view
plaincopyprint?

BOOL FolderExist(CString strPath)  

{  

   WIN32_FIND_DATA wfd;  

    BOOL rValue= FALSE;  

    HANDLE hFind= FindFirstFile(strPath, &wfd);  

    if((hFind!=INVALID_HANDLE_VALUE)&&  

        (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))  

    {  

       rValue = TRUE;  

    }  

   FindClose(hFind);  

    returnrValue;  

}  

 

文件存在性检查:
注意,该函数是检查当前目录下是否有该文件

如果想检查其他目录下是否有该文件,则在参数中输入该文件的完整路径即可

 
[cpp] view
plaincopyprint?

BOOL FileExist(CString strFileName)  

{  

    CFileFindfFind;  

    returnfFind.FindFile(strFileName);  

}  

 

创建目录:

[cpp] view
plaincopyprint?

BOOL CreateFolder(CString strPath)  

{  

   SECURITY_ATTRIBUTES attrib;  

   attrib.bInheritHandle = FALSE;  

   attrib.lpSecurityDescriptor = NULL;  

   attrib.nLength = sizeof(SECURITY_ATTRIBUTES);  

   //上面定义的属性可以省略  

    //直接使用return::CreateDirectory(path, NULL);即可  

    return::CreateDirectory(strPath, &attrib);  

}  

 
 

文件大小:


[cpp] view
plaincopyprint?

DWORDGetFileSize(CString filepath)  

{  

   WIN32_FIND_DATA fileInfo;  

    HANDLEhFind;  

    DWORDfileSize;  

    CStringfilename;  

    filename =filepath;  

    hFind =FindFirstFile(filename,&fileInfo);  

    if(hFind !=INVALID_HANDLE_VALUE)  

       fileSize =fileInfo.nFileSizeLow;  

     

   FindClose(hFind);  

    returnfilesize;  

}  

当然在CFileFind里面有GetLength()函数,也可以求得。

文件夹大小

[cpp] view
plaincopyprint?

DWORDCVCTestDlg::GetDirSize(CString strDirPath)  

{  

    CStringstrFilePath;  

   DWORD   dwDirSize = 0;  

     

    strFilePath+= strDirPath;  

    strFilePath+= "\\*.*";  

     

    CFileFindfinder;  

    BOOL bFind =finder.FindFile(strFilePath);  

    while(bFind)  

    {  

       bFind =finder.FindNextFile();  

       if(!finder.IsDots())  

       {  

          CStringstrTempPath = finder.GetFilePath();  

          if(!finder.IsDirectory())  

          {  

             dwDirSize +=finder.GetLength();  

          }  

          else  

          {  

             dwDirSize +=GetDirSize(strTempPath);  

          }  

       }  

    }  

   finder.Close();  

    returndwDirSize;  

转自:http://blog.csdn.net/wangjieest/article/details/7000640
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: