VC判断一个文件为目录的方法
本文实例讲述了VC判断一个文件为目录的方法,分享给大家供大家参考。具体实现方法如下:
这是一个自定义函数,用于判断一个文件是否为目录:
/** *checkwhetherafileisadirectory @returntrueifisadirectory,elsefalse(iffilenotexists,false) */ __declspec(dllexport)boolIsDirectory(constchar*filename) { DWORDdwAttr=::GetFileAttributes(filename); //得到文件属性 if(dwAttr==0xFFFFFFFF) //文件或目录不存在 returnfalse; elseif(dwAttr&FILE_ATTRIBUTE_DIRECTORY) //如果是目录 returntrue; else returnfalse; }
以下是GetFileAttribute定义,摘自msdn:
RetrievesasetofFATfilesystemattributesforaspecifiedfileordirectory.得到FAT文件系统的文件属性
Parameters
lpFileName
Thenameofthefileordirectory.IntheANSIversionofthisfunction,thenameislimitedtoMAX_PATHcharacters.Toextendthislimitto32,767widecharacters,calltheUnicodeversionofthefunctionandprepend"//?/"tothepath.Formoreinformation,seeNamingaFile.文件名或目录名。最大长度为系统的文件名最大长度。如果是unicode环境,需要调用这个函数的unicode版本。
ReturnValue
Ifthefunctionsucceeds,thereturnvaluecontainstheattributesofthespecifiedfileordirectory.
如果函数成功了,返回值会包含以下文件属性:
Ifthefunctionfails,thereturnvalueisINVALID_FILE_ATTRIBUTES.Togetextendederrorinformation,callGetLastError.
如果函数失败,返回值是INVALID_FILE_ATTRIBUTES.可以通过GetLastError获取更详细的出错信息
Theattributescanbeoneormoreofthefollowingvalues.
文件属性可以是下列值的一个或多个的组合。
FILE_ATTRIBUTE_ARCHIVE Afileordirectorythatisanarchivefileordirectory. Applicationsusethisattributetomarkfilesforbackuporremoval. 存档文件 FILE_ATTRIBUTE_COMPRESSED Afileordirectorythatiscompressed. Forafile,allofthedatainthefileiscompressed. Foradirectory,compressionisthedefaultfornewlycreatedfilesandsubdirectories. 压缩文件 FILE_ATTRIBUTE_DEVICE Reserved;donotuse. FILE_ATTRIBUTE_DIRECTORY Thehandlethatidentifiesadirectory. 目录文件 FILE_ATTRIBUTE_ENCRYPTED Afileordirectorythatisencrypted. Forafile,alldatastreamsinthefileareencrypted. Foradirectory,encryptionisthedefaultfornewlycreatedfilesandsubdirectories. 加密文件 FILE_ATTRIBUTE_HIDDEN Thefileordirectoryishidden.Itisnotincludedinanordinarydirectorylisting. 隐藏文件 FILE_ATTRIBUTE_NORMAL Afileordirectorythatdoesnothaveotherattributesset. Thisattributeisvalidonlywhenusedalone. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED Thefileisnottobeindexedbythecontentindexingservice. FILE_ATTRIBUTE_OFFLINE Thedataofafileisnotavailableimmediately. Thisattributeindicatesthatthefiledataisphysicallymovedtoofflinestorage.ThisattributeisusedbyRemoteStorage,whichisthehierarchicalstoragemanagementsoftware.Applicationsshouldnotarbitrarilychangethisattribute. FILE_ATTRIBUTE_READONLY Afileordirectorythatisread-only. Forafile,applicationscanreadthefile,butcannotwritetoitordeleteit. Foradirectory,applicationscannotdeleteit. FILE_ATTRIBUTE_REPARSE_POINT Afileordirectorythathasanassociatedreparsepoint,orafilethatisasymboliclink. FILE_ATTRIBUTE_SPARSE_FILE Afilethatisasparsefile. FILE_ATTRIBUTE_SYSTEM Afileordirectorythattheoperatingsystemusesapartof,orusesexclusively. FILE_ATTRIBUTE_TEMPORARY Afilethatisbeingusedfortemporarystorage. Filesystemsavoidwritingdatabacktomassstorageifsufficientcachememoryisavailable,becausetypically,anapplicationdeletesatemporaryfileafterthehandleisclosed.Inthatscenario,thesystemcanentirelyavoidwritingthedata.Otherwise,thedataiswrittenafterthehandleisclosed. FILE_ATTRIBUTE_VIRTUAL Afileisavirtualfile.
Returncode/value
Description
32
0x20
2048
0x800
64
0x40
16
0x10
16384
0x4000
2
0x2
128
0x80
8192
0x2000
4096
0x1000
1
0x1
1024
0x400
512
0x200
4
0x4
256
0x100
65536
0x10000
希望本文所述对大家的VC程序设计有所帮助。