声明: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意味着什么也没有。
重要信息:在测试期间,作者通常会收到一个非零的返回代码,即使适当的位已经被成功设置。
{创建一个文本文件,设置为只读与系统,显示文件属性}
var
fileName : string;
myFile : TextFile;
attrs : Integer;
begin
// 尝试以写模式打开ATesstFile.tex
fileName := 'ATestFile.txt';
AssignFile(myFile, fileName);
ReWrite(myFile);
// 写入文件
Write(myFile, 'Hello World');
// 关闭文件
CloseFile(myFile);
// 设置文件为只读文件与系统文件
if FileSetAttr(fileName, faReadOnly or faSysFile) > 0
then ShowMessage('File made into a read only system file')
else ShowMessage('File attribute change failed');
// 取得文件属性
attrs := FileGetAttr(fileName);
// 显示文件属性
if attrs and faReadOnly > 0
then ShowMessage('File is read only')
else ShowMessage('File is not read only');
if attrs and faHidden > 0
then ShowMessage('File is hidden')
else ShowMessage('File is not hidden');
if attrs and faSysFile > 0
then ShowMessage('File is a system file')
else ShowMessage('File is not a system file');
if attrs and faVolumeID > 0
then ShowMessage('File is a volume ID')
else ShowMessage('File is not a volume ID');
if attrs and faDirectory > 0
then ShowMessage('File is a directory')
else ShowMessage('File is not a directory');
if attrs and faArchive > 0
then ShowMessage('File is archived')
else ShowMessage('File is not archived');
if attrs and faSymLink > 0
then ShowMessage('File is a symbolic link')
else ShowMessage('File is not a symbolic link');
end;
var
fileName : string;
myFile : TextFile;
attrs : Integer;
begin
// 尝试以写模式打开ATesstFile.tex
fileName := 'ATestFile.txt';
AssignFile(myFile, fileName);
ReWrite(myFile);
// 写入文件
Write(myFile, 'Hello World');
// 关闭文件
CloseFile(myFile);
// 设置文件为只读文件与系统文件
if FileSetAttr(fileName, faReadOnly or faSysFile) > 0
then ShowMessage('File made into a read only system file')
else ShowMessage('File attribute change failed');
// 取得文件属性
attrs := FileGetAttr(fileName);
// 显示文件属性
if attrs and faReadOnly > 0
then ShowMessage('File is read only')
else ShowMessage('File is not read only');
if attrs and faHidden > 0
then ShowMessage('File is hidden')
else ShowMessage('File is not hidden');
if attrs and faSysFile > 0
then ShowMessage('File is a system file')
else ShowMessage('File is not a system file');
if attrs and faVolumeID > 0
then ShowMessage('File is a volume ID')
else ShowMessage('File is not a volume ID');
if attrs and faDirectory > 0
then ShowMessage('File is a directory')
else ShowMessage('File is not a directory');
if attrs and faArchive > 0
then ShowMessage('File is archived')
else ShowMessage('File is not archived');
if attrs and faSymLink > 0
then ShowMessage('File is a symbolic link')
else ShowMessage('File is not a symbolic link');
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