delphi获取文件或文件夹属性和修改属性 |
您所在的位置:网站首页 › 将文件设置为隐藏属性并取消存档属性 › delphi获取文件或文件夹属性和修改属性 |
语句片段: function showAttr(path:string) : integer; var Attributes:word; begin Attributes := fileGetAttr(PANsiChar(path)); if (Attributes and faHidden) = faHidden then //faHidden 便是"隐藏"属性 begin 语句块 end; if (Attributes and faReadOnly) = faReadOnly then //faReadOnly 只读属性 begin 语句块 end; if (Attributes and faSysFile) = faSysFile then //faSysFile 系统属性 begin 语句块 end; if (Attributes and faArchive) = faArchive then //faSysFile 系统属性 begin 语句块 end; end;
实例:
1、修改隐藏属性的代码片段
var //隐藏d:\132.txt attr:integer; begin attr := fileGetAttr('D:\123.txt'); if not ( (attr and faHidden) = faHidden )then begin fileSetAttr('D:\123.txt',attr or faHidden); MessageBox(0,' 完成','提示',MB_OK); end; end;
2、修改取消隐藏属性的代码片段
var //显示d:\123.txt attr : integer; begin attr := FileGetAttr('D:\123.txt'); if ( (attr and faHidden) = faHidden ) then begin setFileAttributes('D:\123.txt',(attr and (not faHidden))); MessageBox(0,' 完成','提示',MB_OK); end; end;
fileSetAttr 和 setFileAttributes都是修改文件属性,只是前者来自System.SysUtils,后者来自WinAPI.Windows
附文件属性常量 faReadOnly $00000001 Read-only files 只读文件 faHidden $00000002 Hidden files 隐藏文件 faSysFile $00000004 System files 系统文件 faVolumeID $00000008 Volume ID files 卷标文件 faDirectory $00000010 Directory files 目录 faArchive $00000020 Archive files 归档文件 faAnyFile $0000003F Any file 任意文件 取消只需要在常量前面加not,例如:not faReadOnly取消只读文件
procedure TForm1.cxbtn1Click(Sender: TObject); var iAttr: integer; sFileName: string; begin sFileName := 'C:\123.TXT'; iAttr := FileGetAttr(sFileName); if not ((iAttr and faReadOnly) = faReadOnly) then begin if FileSetAttr(sFileName, iAttr or faReadOnly) = 0 then begin MessageBox(0, '修改完成', '提示', MB_OK); end else MessageBox(0, '修改失败', '提示', MB_OK); end else MessageBox(0, '不需要修改', '提示', MB_OK); end;
------------------------------------------------------------------------------------------
1. 在interface下的uses中引用filectrl单元 2. 首先取文件属性 var attr : integer; filename : string; begin filename := 'myfile'; attr := FileGetAttr(filename); end;
3. 设置文件属性(如设置归档属性 -> faArchive ) attr := attr or faArchive; //如要去掉某一属性,则如下句 attr := attr and (not faArchive); //保留其它属性 if FileSetAttr(filename, attr)=0 then //成功代码 else //失败代码 4. 附文件属性常量 Constant Value Description faReadOnly $00000001 Read-only files 只读文件 faHidden $00000002 Hidden files 隐藏文件 faSysFile $00000004 System files 系统文件 faVolumeID $00000008 Volume ID files 卷标文件 faDirectory $00000010 Directory files 目录 faArchive $00000020 Archive files 归档文件 faAnyFile $0000003F Any file 任意文件
备注: 有种偷懒的方法, 如果设置文件属性为隐藏则 FileSetAttr('文件全路径',2), 要是设置为只读+隐藏FileSetAttr('文件全路径',3) |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |