如何移动当前键盘当前宏正在使用中的文件

Delphi删除文件容易,但删除正在使用的文件,那就需要手段了,因为正在使用的文件是不允许被删除的,所以要想知道如何实现,或许你会从下面的代码中得到启发,其实很简单,呵呵,不说了,看代码:
unit Unit1;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtC
FILE_DELETE=1;
FILE_RENAME=2;
TForm1 = class(TForm)
Button1: TB
Label1: TL
Label2: TL
RadioGroup1: TRadioG
Button2: TB
Button3: TB
OpenDialog1: TOpenD
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
{ Private declarations }
{ Public declarations }
Form1: TForm1;
implementation
{$R *.DFM}
function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PCflag:Uint): B
WindowsDirs: array [0..MAX_PATH + 1] of C
lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of C
VerPlatForm: TOSVersionInfoA;
StrLstDelte: TS
filename,s
Result := FALSE;
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
SetLastError(ERROR_NOT_SUPPORTED);
else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
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);
else begin
StrLstDelte := TStringList.C
GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
filename:=WindowsD
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
if length(s)&8 then raise exception.create('不是有效的短文件名(8+3格式)!');
else begin
if (i-1&8)or(length(s)-i&3) then raise exception.create('不是有效的短文件名(8+3格式)!');
strcopy(lpDirDes,lpFileNameToDes);
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.F
procedure TForm1.Button2Click(Sender: TObject);
if OpenDialog1.Execute then
edit1.text:=OpenDialog1.FileN
procedure TForm1.Button3Click(Sender: TObject);
if OpenDialog1.Execute then
edit2.text:=OpenDialog1.FileN
procedure TForm1.Button1Click(Sender: TObject);
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('操作完成');
procedure TForm1.Edit2Change(Sender: TObject);
VerPlatForm: TOSVersionInfoA;
buf: array [0..MAX_PATH + 1] of C
if not fileexists(edit2.text)
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
GetShortPathName(pchar(edit2.text), buf, MAX_PATH + 1);
edit2.text:=
procedure TForm1.RadioGroup1Click(Sender: TObject);
edit2.Enabled:=RadioGroup1.ItemIndex=1;
button2.Enabled:=RadioGroup1.ItemIndex=1;
其实就是利用Windows重启的瞬间来删除或移动文件。
本类推荐文章
本类最新更新
最新源码下载

我要回帖

更多关于 音频流当前正在使用中 的文章

 

随机推荐