本文章来自http://blog.csdn.net/hellogv/
/以下是锁定文件夹的方法
procedure TSysClass.QuickLock(DirList:TStrings); //DirList是保存文件夹的列表
var
UnLockDIRList:TStrings;
i:integer;
IsLock:boolean;
begin
UnLockDIRList:=TStringList.Create;
UnLockDIRList.Add('以下目录锁定失败,请检查有文件是否正被使用或者是空文件夹:');
for i:=0 to DIRList.Count -1 do begin
IsLock:=false;//本来就是锁定失败
if GetDirSize(DirList.Strings[i],true)>0 then //如果文件夹不为空 ,则执行下面语句,如果为空则ISLock=false
IsLock:=RenameFile(DIRList.Strings[i],DIRList.Strings[i]+'../'); //通过修改文件夹名称来检测文件夹是否可以锁定
if IsLock= false then begin //如果锁定失败,则记录目录夹名称
UnLockDIRList.Add(DIRList.Strings[i]);
end;
end;
if UnLockDIRList.Count = 1 then begin //全文件夹成功被锁定
ShowMessage('文件夹锁定成功!');
exit;
end;
if UnLockDIRList.Count > 1 then //部分文件夹没有被锁定
ShowMessage(UnLockDIRList.Text);
UnLockDIRList.Free;
end;
/以下是解锁文件夹的方法
procedure TSysClass.UnLock(DIRList:TStrings);
var
i,ii:integer;
FileList:TStrings;
label loop;
begin
FileList:=TStringList.Create;
for i:=0 to DirList.Count -1 do begin
//-----------------------------先创建锁定文件夹的入口
FileList.Clear;
CreateDir(DirList.Strings[i]+'./') ;//创建锁定文件夹的入口
FindAll(DirList.Strings[i]+'./',FileList);
SysFunctions.FileMove(DirList.Strings[i]);//文件解密成功或不用解密直接解锁文件夹
loop:
end;
ShowMessage('成功解锁文件夹!'+#10+'如果被锁定的文件夹还没有被删除'+#10+'请不要手动删除'+#10+'本程序关闭时会自动删除!');
end;
procedure TSysClass.FindAll(path: String; var fileresult: TStrings);//查找文件 ,path不带'/'
var
fpath,s: String;
fs: TsearchRec;
i:integer;
begin
fpath:=path+'/*.*';
if FindFirst(fpath,faAnyFile,fs)=0 then
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(path+'/'+fs.Name,fileresult)
else
fileresult.add(path+'/'+fs.Name);
while findnext(fs)=0 do
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
Findall(path+'/'+fs.Name,fileresult)
else begin
fileresult.add(path+'/'+fs.Name);
end;
end;
end;
Findclose(fs);
end;