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;

解决方案 »

  1.   

    可以参考我在kingron的主页kingron.myetang.com上提供的一个小例子的代码。
      

  2.   

    先用FileSetAttr()将属性改为0。
    FileSetAttr(ProcessFile, 0);
    DeleteFile(ProcessFile);
      

  3.   

    to all 我的程作了以下修该
    if FileExists(ProcessFile) then
        begin
         showmessage('ok');
         FileSetAttr(ProcessFile, 0);
         if  DeleteFile(ProcessFile) then
          Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);
        end;可以显示OK,可就是不能成功删除文件!!!怪!!!!!请指教!!
    同时还用了chechy(我爱洁洁) 的函数,也不行哟:((
      

  4.   

    整个函数如下:
    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;
      

  5.   

    调用Earse(Filename)函数不就可以么?
      

  6.   

    to liuting(游子)我已经用KillProcess杀死进程了!!
    还是不能删除文件
      

  7.   

    to icd(骆驼) 当然可以拉!!!要不就不问你们这些高手拉!
      

  8.   


    if FileExists('e:\1.exe') then
       if  DeleteFile('e:\1.exe') then
           Application.MessageBox(PChar('成功删除文件!!!'),'反馈信息',MB_Ok+MB_ICONINFORMATION);我试了没问题,能删掉,
      

  9.   

    to icd(骆驼) 这个我自己也试过拉!!!当然可以拉!
    不过在我的程序里面就不行了!所以才问你们的!:)
    我的程序如上,是一个杀死进程,并删除进程文件的程序子过程!
    还望高手指正!
    整个函数如下:
    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,就是不能删除文件!
      

  10.   

    to  wylove(阿刚)杀死进程没有成功这个是不可能的:(((!这个我自己考虑过了!
    不能杀死进程的话processlists组件不会删除item!
      

  11.   

    看这个能不能帮你 
    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. 
     
      

  12.   

    to dy_kiss(龙之天涯) 真搞笑!
      

  13.   

    用API SHFileOperation 需要传递TSHFILEOPSTRUCT类型的参数,如下:
    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;
      

  14.   

    procedure TForm1.Button1Click(Sender: TObject);var
      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;
    应该很容易删除的
      

  15.   

    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);
    不知道各位高手还有什么更好的解决办法没有!
    我个人认为是杀死进程后,进程资源还没有释放干净哟!!!??
      

  16.   

    to quejian(左巴) 后来你怎么解决的?我怀疑是杀死进程后,资源还没有释放干净的原因吧!!!!??????????????说说你的思路哟
      

  17.   

    DeleteFile()好像是要提供完全的路径,的。
      

  18.   

    你那是什么文件啊。既然DeleteFile(ProcessFile)成功了,文件就应该删除了。如果你删除的是System32目录下的EXE和DLL文件,系统可能已经在system32\dllcache下做了备份。所以你删除文件后,系统又给恢复过来了。
      

  19.   

    to all
    我的问题至今还没有解决拉!!!!!!!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);
    不知道各位高手还有什么更好的解决办法没有!
    我个人认为是杀死进程后,进程资源还没有释放干净哟!!!??
      

  20.   

    KillProcess;//杀死进程 
    应该有一个援时所以不能马上删除,应该等待几秒之后才行。
      

  21.   

    在单元里引入winsows或用.window.deletefile
      

  22.   

    KillProcess 是你自己写的函数吧。你用sleep(10000);实际是在等待进程的结束。如果你已经
    得到了进程的句柄,可以用WaitForSingleObject(hHandle,INFINITE)等待进程结束,而不必要
    用sleep来等待(等待的时间无法掌握)。
      

  23.   

    to jishiniping
    明白了!怎样有进程ID得到进程句柄:)?
      

  24.   

    to jishiping(JSP 季世平) 
    我明白了,怎样由进程ID得到进程句柄?:)??
      

  25.   

    to jishiping(JSP 季世平) 
    processhandl:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE,FALSE,dword(ProcessID));
    其中processid是一个进程的ID,
    processhandl就是你想要的句柄了
    这样对吗?
      

  26.   

    to  jishiping(JSP 季世平) 
    上面的代码其实不能通过,不过我已经从你的提示中获得了灵感!
    哈哈哈哈哈哈哈哈哈哈哈哈,这个捆饶我一两个月的问题,
    终于圆满得得到了解决!
    如果你要知道我最后怎么解决的,请跟我联系哟:)
    哈哈哈哈哈哈哈,今天好高兴呀。
    我决定结束这个帖子了,分都给你哟,因为虽然你没有帮助我
    直接解决问题,可是让我获得的灵感,写写了:)
    好消息,我已经解决问题了