雨过天晴 发表于 2017-6-2 13:28:31

检查某个文件或目录是否存在的函数

RT


#include <Windows.h>

BOOL FileExists(LPCTSTR lpszFileName, BOOL bIsDirCheck)
{
        //试图取得文件属性
        DWORD dwAttributes = ::GetFileAttributesA(lpszFileName);
        if ( INVALID_FILE_ATTRIBUTES == dwAttributes)
        {
                return FALSE;
        }

        //是目录
        if (dwAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)
        {
                if (bIsDirCheck) //当前检测的也是目录
                {
                        return TRUE;
                }
                else       
                        return FALSE;
        }
        else //是文件
        {
                if (bIsDirCheck)
                {
                        return FALSE;
                }
                else       
                        return TRUE;
        }
       
}

int main(int argc, char *argv[])
{
        BOOL bRetDir, bRetFile;
        //测试,一个目录
        bRetDir = FileExists("C:\\11\\", TRUE);
        //测试, 一个文件
        bRetFile = FileExists("C:\\11\\1.xls", FALSE);       
        return 0;
}
页: [1]
查看完整版本: 检查某个文件或目录是否存在的函数