0%

File Attribute

文件属性的读取

文件属性的一些读取,主要就是依靠。

GetFileType获取文件类型

就一个参数,文件句柄,CreateFile获得的,返回值:

Value Meaning
FILE_TYPE_UNKNOWN The type of the specified file is unknown.
FILE_TYPE_DISK The specified file is a disk file.
FILE_TYPE_CHAR The specified file is a character file, typically an LPT device or a console.
FILE_TYPE_PIPE The specified file is either a named or anonymous pipe.

GetFileSize获取文件尺寸

这个函数用来获取文件的大小,在读取文件的时候很好用

如果函数成功,则返回的值是文件大小的低阶双字,如果lpFileSizeHigh是非null,则函数将文件大小的高阶双字放入该参数指向的变量中。
如果函数失败且lpFileSizeHigh为NULL,则返回值为0xFFFFFFFF。要获取扩展的错误信息,请调用GetLastError。
如果函数失败并且lpFileSizeHigh是非空的,则返回值为0xFFFFFFFF, GetLastError将返回一个NO_ERROR以外的值。

返回值的一个判定,简单看看就好了。

GetFileAttributes获取文件属性

这个东西不复杂就是有点乱,参数就是一个字符串,不要忘记转义就好了。
看一下MSDN的一些说明。

Attribute Meaning
FILE_ATTRIBUTE_ARCHIVE The file or directory is an archive file or directory. Applications use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_COMPRESSED The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_DIRECTORY The handle identifies a directory.
FILE_ATTRIBUTE_ENCRYPTED The file or directory is encrypted. For a file, this means that all data streams are encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories.
FILE_ATTRIBUTE_HIDDEN The file or directory is hidden. It is not included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL The file or directory has no other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_OFFLINE The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.
FILE_ATTRIBUTE_READONLY The file or directory is read-only. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it.
FILE_ATTRIBUTE_REPARSE_POINT The file has an associated reparse point.
FILE_ATTRIBUTE_SPARSE_FILE The file is a sparse file.
FILE_ATTRIBUTE_SYSTEM The file or directory is part of, or is used exclusively by, the operating system.
FILE_ATTRIBUTE_TEMPORARY The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.
(呼~,终于写完了,手都累酸了)

返回值就是这样子判断,这些东西对与目录一样也是适用的。
示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
DWORD dwAttributes = GetFileAttributes("Wker.txt") ;
if(dwAttributes == 0XFFFFFFFF)
{
cout<<"这是个什么东西?";
return -1;
}
if((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
cout<<"啊!是个目录啊";
}else
{
cout<<"啊!是个文件啊";
}
return 0;
}

设置文件属性:SetFileAttributes

1
2
3
4
BOOL SetFileAttributes(
LPCTSTR lpFileName, // pointer to filename
DWORD dwFileAttributes // attributes to set
);

参数就不介绍了,很简单,第二个参数值太多,百度一下就可以了。