声明:function FileSetAttr ( const FileName : string; Attributes : Integer ) : Integer;
描述:FileSetAttr函数设置指定文件FileName的属性。
Attributes整数必须设为下列独立位的0个,多个或所有设置的联合。
faReadOnly:1:只读文件
faHidden:2:隐藏文件
faSysFile:4:系统文件
faVolumeID:8:卷标文件
faDirectory:16:目录文件
faArchive:32:存档文件
faSymLink:64:符号连接
如果设置功,则返回值为0,否则它包含一个错误代码。
备注:这个函数是依赖于操作系统的,比如在Linux下,Archive意味着什么也没有。
重要信息:在测试期间,作者通常会收到一个非零的返回代码,即使适当的位已经被成功设置。
![ExpandedBlockEnd.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockEnd.gif)
{创建一个文本文件,设置为只读与系统,显示文件属性}
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
var
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
fileName :
string;
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
myFile : TextFile;
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
attrs : Integer;
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
begin
// 尝试以写模式打开ATesstFile.tex
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
fileName := 'ATestFile.txt';
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
AssignFile(myFile, fileName);
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
ReWrite(myFile);
// 写入文件
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
Write(myFile, 'Hello World');
// 关闭文件
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
CloseFile(myFile);
// 设置文件为只读文件与系统文件
if FileSetAttr(fileName, faReadOnly or faSysFile) > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File made into a read only system file')
else ShowMessage('File attribute change failed');
// 取得文件属性
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
attrs := FileGetAttr(fileName);
// 显示文件属性
if attrs and faReadOnly > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is read only')
else ShowMessage('File
is not read only');
if attrs and faHidden > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is hidden')
else ShowMessage('File
is not hidden');
if attrs and faSysFile > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is a system file')
else ShowMessage('File
is not a system file');
if attrs and faVolumeID > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is a volume ID')
else ShowMessage('File
is not a volume ID');
if attrs and faDirectory > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is a directory')
else ShowMessage('File
is not a directory');
if attrs and faArchive > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is archived')
else ShowMessage('File
is not archived');
if attrs and faSymLink > 0
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
then ShowMessage('File
is a symbolic link')
else ShowMessage('File
is not a symbolic link');
![None.gif](http://info.52z.com/editor/fckeditor/editor/dialog/InsertCode/codeimages/None.gif)
end;
程序运行结果:
File made into a read only system file
File is read only
File is not hidden
File is a system file
File is not a Volume ID
File is not a directory
File is not archived
File is not a symbolic link