function DelDirectory(const Source:string): boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
解决方案 »
- 利用宽带开发网络软件的问题
- 请教: 如果有触发器的话, 插入数据时数据明显变慢. 有没有改进的办法?
- 播放WMV文件时怎么转换声道?CSDN没人了吗?
- 奇怪:在Delphi5下编译过的程序,在Delphi6下编译报错:[Error] Ujf_ybzxf.pas(310): Undeclared identifier: 'VarArrayOf'
- 有支持delphi7 的fastreport吗
- 关闭窗体,显示提示信息方面
- 奇怪,我在对一个表打开地时候出现提示:!
- 我就不相信,这200分.......
- 能否通过Tmsg 的Wparam 和 Lparam 来判断按下组合肩(如 Ctrl + F11)
- 如何在一报表中动态创建QRLabel组建???高分请进……SOS
- delphi5的update1如何安装?
- 出现variant array index out of bounds错误
FileSetAttr(ProcessFile, 0);
DeleteFile(ProcessFile);
if FileExists(ProcessFile) then
begin
showmessage('ok');
FileSetAttr(ProcessFile, 0);
if DeleteFile(ProcessFile) then
Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);
end;可以显示OK,可就是不能成功删除文件!!!怪!!!!!请指教!!
同时还用了chechy(我爱洁洁) 的函数,也不行哟:((
var
ProcessFile : string;
begin
ProcessFile:=ProcessLists.Selected.Caption;
if Application.MessageBox(PChar('真的要终止【'+ProcessFile+'】,'+CRLF+'并删除此文件吗?'),'操作确认',
MB_Ok+MB_OKCANCEL+MB_ICONSTOP)=1 then
begin
KillProcess;//杀死进程
if FileExists(ProcessFile) then
begin
shomessage('ok');
FileSetAttr(ProcessFile, 0);
FileSetAttr(ProcessFile, 0);
if DeleteFile(ProcessFile) then
Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);
end;
end;
end;
还是不能删除文件
if FileExists('e:\1.exe') then
if DeleteFile('e:\1.exe') then
Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);我试了没问题,能删掉,
不过在我的程序里面就不行了!所以才问你们的!:)
我的程序如上,是一个杀死进程,并删除进程文件的程序子过程!
还望高手指正!
整个函数如下:
var
ProcessFile : string;
begin
ProcessFile:=ProcessLists.Selected.Caption;
if Application.MessageBox(PChar('真的要终止【'+ProcessFile+'】,'+CRLF+'并删除此文件吗?'),'操作确认',
MB_Ok+MB_OKCANCEL+MB_ICONSTOP)=1 then
begin
KillProcess;//杀死进程
if FileExists(ProcessFile) then
begin
shomessage('ok');
FileSetAttr(ProcessFile, 0);
if DeleteFile(ProcessFile) then
Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);
end;
end;
可以显示OK,就是不能删除文件!
不能杀死进程的话processlists组件不会删除item!
unit Unit1;interfaceuses
filectrl,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
function MoveDir(sDirName:String;sToDirName:string):Boolean;
function DoCopyDir(sDirName:String;sToDirName:String):Boolean;
function CopyDir(sDirName:String;sToDirName:string):Boolean;
function DoRemoveDir(sDirName:String):Boolean;
function DeleteDir(sDirName:String):Boolean;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}function TForm1.DoCopyDir(sDirName:String;sToDirName:String):Boolean;
var
hFindFile:Cardinal;
t,tfile:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
//先保存当前目录
sCurDir:=GetCurrentDir;
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
if sToDirName[Length(sToDirName)]='\' then
t:=copy(sToDirName,1,length(sToDirName)-1);
//检查最后一个字符是否是'\'
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
Continue; //每个目录下
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
if sToDirName[Length(sToDirName)]<>'\' then
t:=sToDirName+'\'+tfile
else
t:=sToDirName+tfile;
//检查最后一个字符是否是'\' if not DirectoryExists(t) then
ForceDirectories(t);
//目标处建立目录
if sDirName[Length(sDirName)]<>'\' then
DoCopyDir(sDirName+'\'+tfile,t)
else
DoCopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'\'+tFile;
CopyFile(PChar(tfile),PChar(t),True);
end;
until FindNextFile(hFindFile,FindFileData)=false;
windows.FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
result:=true;
end;//---- 1.2拷贝目录的函数:CopyDirfunction TForm1.CopyDir(sDirName:String;sToDirName:string):Boolean;
begin
if Length(sDirName)<=0 then
exit;
//拷贝...
Result:=DoCopyDir(sDirName,sToDirName);
end;
{
---- 2、删除目录---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。---- 2.1删除目录的递归辅助函数:DoRemoveDir }function TForm1.DoRemoveDir(sDirName:String):Boolean;
var
hFindFile:Cardinal;
tfile:String;
sCurDir:String;
bEmptyDir:Boolean;
FindFileData:WIN32_FIND_DATA;
stt:string;
begin
//如果删除的是空目录,则置bEmptyDir为True
//初始时,bEmptyDir为True
bEmptyDir:=True;
//先保存当前目录
sCurDir:=GetCurrentDir;
showmessage(sCurDir);
SetLength(sCurDir,Length(sCurDir));
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
begin
bEmptyDir:=bEmptyDir and True;
Continue;
end;
//不是空目录,置bEmptyDir为False
bEmptyDir:=False;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
if sDirName[Length(sDirName)]<>'\' then
DoRemoveDir(sDirName+'\'+tfile)
else
DoRemoveDir(sDirName+tfile);
if not RemoveDirectory(PChar(tfile)) then
result:=false
else
result:=true;
end
else
begin
if not DeleteFile(PChar(tfile)) then
result:=false
else
result:=true;
end;
until FindNextFile(hFindFile,FindFileData)=false;
windows.FindClose(hFindFile); removedir(stt);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//如果是空目录,则删除该空目录
if bEmptyDir then
begin
//返回上一级目录
ChDir('..');
//删除空目录
RemoveDirectory(PChar(sDirName));
end; //回到原来的目录下
ChDir(sCurDir);
result:=true;
end;//---- 2.2删除目录的函数:DeleteDirfunction Tform1.DeleteDir(sDirName:String):Boolean;
begin
if Length(sDirName)<=0 then
exit;
//删除...
Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
end;//---- 3、移动目录//---- 有了拷贝目录和删除目录的函数,移动目录就变得很简单,只需顺序调用前两个函数即可:function TForm1.MoveDir(sDirName:String;sToDirName:string):Boolean;
begin
if CopyDir(sDirName,sToDirName) then
if RemoveDir(sDirName) then
result:=True
else
result:=false;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
copydir('d:\tt\tt','f:\ff\ff');end;procedure TForm1.Button2Click(Sender: TObject);
begin
deleteDir('f:\ff\ff');
end;
end.
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
F: Textfile;
begin
OpenDialog1.Title := 'Delete File';
if OpenDialog1.Execute then begin
AssignFile(F, OpenDialog1.FileName);
try
Reset(F);
if MessageDlg('Erase ' + OpenDialog1.FileName + '?',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
CloseFile(F);
Erase(F);
end;
except
on EInOutError do MessageDlg('File I/O error.', mtError, [mbOk], 0);
end;
end;
end;
应该很容易删除的
ProcessFile : string;
begin
ProcessFile:=ProcessLists.Selected.Caption;
if Application.MessageBox(PChar('真的要终止【'+ProcessFile+'】,'+CRLF+'并删除此文件吗?'),'操作确认',
MB_Ok+MB_OKCANCEL+MB_ICONSTOP)=1 then
begin
KillProcess;//杀死进程
//添加此句后可以删除文件拉!!!!
sleep(10000);
if FileExists(ProcessFile) then
begin
shomessage('ok');
FileSetAttr(ProcessFile, 0);
if DeleteFile(ProcessFile) then
Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);
end;
end;
可以显示OK,就是不能删除文件!
//添加此句后可以删除文件拉!!!!
sleep(10000);
不知道各位高手还有什么更好的解决办法没有!
我个人认为是杀死进程后,进程资源还没有释放干净哟!!!??
我的问题至今还没有解决拉!!!!!!!var
ProcessFile : string;
begin
ProcessFile:=ProcessLists.Selected.Caption;
if Application.MessageBox(PChar('真的要终止【'+ProcessFile+'】,'+CRLF+'并删除此文件吗?'),'操作确认',
MB_Ok+MB_OKCANCEL+MB_ICONSTOP)=1 then
begin
KillProcess;//杀死进程
//添加此句后可以删除文件拉!!!!
sleep(10000);
if FileExists(ProcessFile) then
begin
shomessage('ok');
FileSetAttr(ProcessFile, 0);
if DeleteFile(ProcessFile) then
Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);
end;
end;
可以显示OK,就是不能删除文件!
//添加此句后可以删除文件拉!!!!
sleep(10000);
不知道各位高手还有什么更好的解决办法没有!
我个人认为是杀死进程后,进程资源还没有释放干净哟!!!??
应该有一个援时所以不能马上删除,应该等待几秒之后才行。
得到了进程的句柄,可以用WaitForSingleObject(hHandle,INFINITE)等待进程结束,而不必要
用sleep来等待(等待的时间无法掌握)。
明白了!怎样有进程ID得到进程句柄:)?
我明白了,怎样由进程ID得到进程句柄?:)??
processhandl:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE,FALSE,dword(ProcessID));
其中processid是一个进程的ID,
processhandl就是你想要的句柄了
这样对吗?
上面的代码其实不能通过,不过我已经从你的提示中获得了灵感!
哈哈哈哈哈哈哈哈哈哈哈哈,这个捆饶我一两个月的问题,
终于圆满得得到了解决!
如果你要知道我最后怎么解决的,请跟我联系哟:)
哈哈哈哈哈哈哈,今天好高兴呀。
我决定结束这个帖子了,分都给你哟,因为虽然你没有帮助我
直接解决问题,可是让我获得的灵感,写写了:)
好消息,我已经解决问题了