如题,望高手赐教, 因为我在设计软件在线更新时需要删除自身或者备份自身
解决方案 »
- 請各位高手專家幫幫忙...謝謝!...
- 高手救命,DBGird里用picklist实现下拉框,问题是:当要输入时按向下的方向键却不能选择,而是跑到下一条记录(新建一条记录)了,请问,如
- 关于fastreport对话框问题(在线等待!),解决马上结贴!
- QUICKREPORT中的QREXPR控件
- 线程互扰?
- 关于savedialog1.FileName的值是乱码
- 一个简单的SQL语句问题,关于分组,答对马上给分。
- 关于数据库的问题,如何用SQL语句修改数据表中的字段长度?(不删除原表)
- 关于字体问题
- dspack如何设置摄像头视频质量
- TRichEdit怎样新增的内容到最后一行?
- 怎么得到系统的时间和日期??
const
FILE_DELETE=1;
FILE_RENAME=2;
这个函数用来删除或移动
Function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
var
WindowsDirs: array [0..MAX_PATH + 1] of Char;
lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
VerPlatForm: TOSVersionInfoA;
StrLstDelte: TStrings;
filename,s :String;
i:integer;
begin
Result := FALSE;
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
begin
SetLastError(ERROR_NOT_SUPPORTED);
Exit;
end
else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
if flag=FILE_DELETE then
Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
else if (flag=FILE_RENAME) then
Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
end
else begin
StrLstDelte := TStringList.Create;
GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
filename:=WindowsDirs;
if filename[length(filename)]<>'\' then filename:=filename+'\';
filename:=filename+'wininit.ini';
if FileExists(filename) then
StrLstDelte.LoadFromFile(filename);
if StrLstDelte.IndexOf('[rename]') = -1 then
StrLstDelte.Add('[rename]');
GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
if fileexists(lpFileNameToDes) then
GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
else begin
s:=extractfilename(lpFileNameToDes);
i:=pos('.',s);
if (i=0) then
begin
if length(s)>8 then raise exception.create('不是有效的短文件名(8+3格式)!');
end
else begin
if (i-1>8)or(length(s)-i>3) then raise exception.create('不是有效的短文件名(8+3格式)!');
end;
strcopy(lpDirDes,lpFileNameToDes);
end;
if (flag=FILE_DELETE) then {删除}
StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, 'NUL='+string(lpDirSrc))
else if (flag=FILE_RENAME) then {改名}
StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + 1, string(lpDirDes)+'='+string(lpDirSrc)); StrLstDelte.SaveToFile(filename);
Result := TRUE;
StrLstDelte.Free;
end;
end;调用方式:
edit1表示源文件,
edit2表示目标文件。
procedure TForm1.Button1Click(Sender: TObject);
var
i:uint;
begin
if RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
else i:=FILE_RENAME;
if edit1.text='' then raise exception.create('源文件为空!');
if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
showmessage('出错了')
else showmessage('ok');
end;