有人知道delphi中删除文件夹和运行文件的函数吗?

解决方案 »

  1.   

    ---- 2、删除目录 ---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。 ---- 2.1删除目录的递归辅助函数:DoRemoveDir function DoRemoveDir(sDirName:String):Boolean;
    var
       hFindFile:Cardinal;
       tfile:String;
       sCurDir:String;
       bEmptyDir:Boolean;
       FindFileData:WIN32_FIND_DATA;
    begin
       //如果删除的是空目录,则置bEmptyDir为True
       //初始时,bEmptyDir为True
       bEmptyDir:=True;
       //先保存当前目录
       sCurDir:=GetCurrentDir;
       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;
            FindClose(hFindFile);
       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删除目录的函数:DeleteDir function DeleteDir(sDirName:String):Boolean;
    begin
          if Length(sDirName)< =0 then
             exit;
          //删除...
          Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if FileExists(ShellListView1.SelectedFolder.PathName) then  // 判断是不是文件
        ShellExecute(0, 'open', PChar(ShellListView1.SelectedFolder.PathName), nil, nil, SW_SHOWNORMAL);
    end;
      

  3.   


    RemoveDirectory ('要删除的目录名',0) ;WinExec('要运行文件名',0) ;