声明: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意味着什么也没有。

重要信息:在测试期间,作者通常会收到一个非零的返回代码,即使适当的位已经被成功设置。

delphi FileSetAttr 设置文件的属性-转{创建一个文本文件,设置为只读与系统,显示文件属性}
delphi FileSetAttr 设置文件的属性-转var
delphi FileSetAttr 设置文件的属性-转  fileName : string;
delphi FileSetAttr 设置文件的属性-转  myFile   : TextFile;
delphi FileSetAttr 设置文件的属性-转  attrs    : Integer;
delphi FileSetAttr 设置文件的属性-转begin
delphi FileSetAttr 设置文件的属性-转  // 尝试以写模式打开ATesstFile.tex
delphi FileSetAttr 设置文件的属性-转  fileName := 'ATestFile.txt';
delphi FileSetAttr 设置文件的属性-转  AssignFile(myFile, fileName);
delphi FileSetAttr 设置文件的属性-转  ReWrite(myFile);
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  // 写入文件
delphi FileSetAttr 设置文件的属性-转  Write(myFile, 'Hello World');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  // 关闭文件
delphi FileSetAttr 设置文件的属性-转  CloseFile(myFile);
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  // 设置文件为只读文件与系统文件
delphi FileSetAttr 设置文件的属性-转  if FileSetAttr(fileName, faReadOnly or faSysFile) > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File made into a read only system file')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File attribute change failed');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  // 取得文件属性
delphi FileSetAttr 设置文件的属性-转  attrs := FileGetAttr(fileName);
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  // 显示文件属性
delphi FileSetAttr 设置文件的属性-转  if attrs and faReadOnly > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is read only')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not read only');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  if attrs and faHidden > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is hidden')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not hidden');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  if attrs and faSysFile > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is a system file')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not a system file');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  if attrs and faVolumeID > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is a volume ID')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not a volume ID');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  if attrs and faDirectory > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is a directory')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not a directory');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  if attrs and faArchive > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is archived')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not archived');
delphi FileSetAttr 设置文件的属性-转  
delphi FileSetAttr 设置文件的属性-转  if attrs and faSymLink > 0
delphi FileSetAttr 设置文件的属性-转  then ShowMessage('File is a symbolic link')
delphi FileSetAttr 设置文件的属性-转  else ShowMessage('File is not a symbolic link');
delphi FileSetAttr 设置文件的属性-转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

相关文章: