发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
Delphi删除文件容易,但删除正在使用的文件,那就需要手段了,因为正在使用的文件是不允许被删除的,所以要想知道如何实现,或许你会从下面的代码中得到启发,其实很简单,呵呵,不说了,看代码:
001
unit Unit1;
002
interface
003
uses
004
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
005
StdCtrls, ExtCtrls;
006
const
007
FILE_DELETE=1;
008
FILE_RENAME=2;
009
type
010
TForm1 =
class
(TForm)
011
Button1: TButton;
012
Label1: TLabel;
013
Label2: TLabel;
014
RadioGroup1: TRadioGroup;
015
Edit1: TEdit;
016
Edit2: TEdit;
017
Button2: TButton;
018
Button3: TButton;
019
OpenDialog1: TOpenDialog;
020
procedure Button2Click(Sender: TObject);
021
procedure Button3Click(Sender: TObject);
022
procedure Button1Click(Sender: TObject);
023
procedure Edit2Change(Sender: TObject);
024
procedure RadioGroup1Click(Sender: TObject);
025
private
026
{ Private declarations }
027
public
028
{ Public declarations }
029
end;
030
var
031
Form1: TForm1;
032
implementation
033
{$R *.DFM}
034
function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
035
036
WindowsDirs: array [0..MAX_PATH + 1] of Char;
037
lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
038
VerPlatForm: TOSVersionInfoA;
039
StrLstDelte: TStrings;
040
filename,s :String;
041
i:integer;
042
begin
043
Result := FALSE;
044
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
045
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
046
GetVersionEx(VerPlatForm);
047
if
VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
048
049
SetLastError(ERROR_NOT_SUPPORTED);
050
Exit;
051
end
052
else
VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
053
054
flag=FILE_DELETE then
055
Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
056
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
057
(flag=FILE_RENAME) then
058
Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
059
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
060
061
062
StrLstDelte := TStringList.Create;
063
GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
064
filename:=WindowsDirs;
065
filename[length(filename)]<>
'\' then filename:=filename+'
\';
066
filename:=filename+
'wininit.ini'
;
067
FileExists(filename) then
068
StrLstDelte.LoadFromFile(filename);
069
StrLstDelte.IndexOf(
'[rename]'
) = -1 then
070
StrLstDelte.Add(
);
071
GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
072
fileexists(lpFileNameToDes) then
073
GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
074
075
s:=extractfilename(lpFileNameToDes);
076
i:=pos(
'.'
,s);
077
(i=0) then
078
079
length(s)>8 then raise exception.create(
'不是有效的短文件名(8+3格式)!'
080
081
082
(i-1>8)or(length(s)-i>3) then raise exception.create(
083
084
strcopy(lpDirDes,lpFileNameToDes);
085
086
(flag=FILE_DELETE) then {删除}
087
StrLstDelte.Insert(StrLstDelte.IndexOf(
) + 1,
'NUL='
+
string
(lpDirSrc))
088
(flag=FILE_RENAME) then {改名}
089
(lpDirDes)+
'='
(lpDirSrc));
090
091
StrLstDelte.SaveToFile(filename);
092
Result := TRUE;
093
StrLstDelte.Free;
094
095
096
procedure TForm1.Button2Click(Sender: TObject);
097
098
OpenDialog1.Execute then
099
edit1.text:=OpenDialog1.FileName;
100
101
102
procedure TForm1.Button3Click(Sender: TObject);
103
104
105
edit2.text:=OpenDialog1.FileName;
106
107
procedure TForm1.Button1Click(Sender: TObject);
108
109
i:
uint
110
111
RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
112
i:=FILE_RENAME;
113
edit1.text=
''
then raise exception.create(
'源文件为空!'
114
(i=FILE_RENAME)and(edit2.text=
) then raise exception.create(
'目标文件为空!'
115
not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
116
showmessage(
'出错了'
)
117
'操作完成'
118
119
120
procedure TForm1.Edit2Change(Sender: TObject);
121
122
123
buf: array [0..MAX_PATH + 1] of Char;
124
125
not fileexists(edit2.text) then exit;
126
127
128
129
VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
130
131
GetShortPathName(pchar(edit2.text), buf, MAX_PATH + 1);
132
edit2.text:=buf;
133
134
135
procedure TForm1.RadioGroup1Click(Sender: TObject);
136
137
edit2.Enabled:=RadioGroup1.ItemIndex=1;
138
button2.Enabled:=RadioGroup1.ItemIndex=1;
139
140
end.
其实就是利用Windows重启的瞬间来删除或移动文件。
来自: quasiceo > 《待分类1》
0条评论
发表
请遵守用户 评论公约
delphi 文件的操作:重命名、复制、移动、删除 | 文件
procedure Edit2Change(Sender: TObject);if MessageDlg(''你确定要替换所有文件中的字符:''+#+''" ''+edit1.text+''" 替换成:"''+ed...
Delphi的图形处理(一)
Delphi的图形处理 之一 -- 图像处理在可视化编程中的作用及其应用价值。第一章 图像处理在可视化编程中的作用及其应用价值。...
Delphi 7中 ActionMainMenuBar、ActionManager、Action、actionlist的用法
Delphi 7中 ActionMainMenuBar、ActionManager、Action、actionlist的用法 Delphi 7中 ActionMainMenuBar、ActionManager、Action、actionlist的用法有以下的例子 unit main;// Find the Reopen item ...
Delphi 中自做动态显示的控件
步骤: 一、新建一个文件 创建一个新的工程(New Project)和空白窗体(Form1),将Form1的Caption属性改为"我的点歌台",在Form1中加入两个Comobobox构件,一个用来显示歌曲所在的专辑,...
Delphi用BusinessSkinForm 做界面皮肤的使用说明
4、修改bsStoredSkin1的filename属性,指向一个皮肤文件,例如我的55套皮肤包是放在C:\Program Files\Borland\Delphi7\BSF-Skins\Skins文件夹里,我就修改。bsStoredSkin1的filename属性为C:\Program F...
Delphi中异常的截获及其个性化处理
程序示例如下: 例1 Procedure TForm1.Button1Click(Sender :TObject) Var Num: Integer; Begin Try Num:=StrToInt(Edit1.Text); Edit2.Text:=IntToStr(Num*Num);...
一周的工作1天干完/python+Delphi自动化记录
有一个超巨型的资料性的文件,是网页上查询之后的结果,有几百页、几百笔资料,需要把里面的资料一条一条的列在一个单独立的文件里面,...
delphi 创建数据库配置文件(TIniFile)
delphi 创建数据库配置文件(TIniFile)Delphi TIniFile 类 操作ini配置文件2008年04月03日 星期四 上午 11:20.--- 上面这一行语句将会为变量myinifile与具体的文件 program.ini建立联系,然后,就可以通...
Delphi小技巧杂记
答: procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key=vk_down then perform(WM_NEXTDLGCTL,0,0) else if key=vk_up then perform(WM_NEXTDLGCTL...
微信扫码,在手机上查看选中内容