delphi FileSetAttr 设置文件的属性-转

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

程序运行结果:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值