求教一个删除目录的问题...... (200分)

X

xeen

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序每查询一次就要生成一个临时目录在系统临时目录下,所以在<br>查询前要把它删除掉。我要API SHFileOperation<br>procedure tscform.deldir(str:string);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; T:TSHfileOpStruct;<br>&nbsp; &nbsp; &nbsp; &nbsp; P:String;<br>begin<br>&nbsp; P:=copy(str,1,length(str)-1);<br>&nbsp; with T do<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;Wnd := 0;<br>&nbsp; &nbsp; &nbsp;wFunc := FO_DELETE;<br>&nbsp; &nbsp; &nbsp;pFrom := pchar(p);<br>&nbsp; &nbsp; &nbsp;pTo := nil;<br>&nbsp; &nbsp; &nbsp;fFlags := FOF_ALLOWUNDO;<br>&nbsp; end;<br>&nbsp; SHFileOperation(T);<br><br>end;<br>在win98下没问题,在win2000下却删不了,报告<br>"无法删除文件:无法读源文件或磁盘",而我试着在win2000中手动却可以删除它<br>这是怎么回事???
 
一样的问题<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=761564<br>
 
procedure RemoveRandomDirectory(sDirName: String; bWin:Boolean; iDir: Byte);<br>var sTmpDirName:String;<br>&nbsp; &nbsp; &nbsp;bDone:Boolean;<br>&nbsp; &nbsp; &nbsp;windir: Array [0..254] of Char;<br>begin<br>&nbsp; if bWin then<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowsDirectory(windir, 255);<br>&nbsp; &nbsp; sDirName := StrPas(windir);<br>&nbsp; end;<br>&nbsp; while ((not bDone) and (iDir &lt; MAX_DIRECTORY))do<br>&nbsp; begin<br>&nbsp; &nbsp; sTmpDirName := sDirName + '/' + format('%s%-.4d.dir', [DIRECTONARY_TITLE, iDir]);<br>&nbsp; &nbsp; Inc(iDir);<br>&nbsp; &nbsp; bDone := not DirectoryExists(sTmpDirName);<br>&nbsp; &nbsp; if not bDone then<br>&nbsp; &nbsp; &nbsp; DeleteDirectory(sTmpDirName, '*.*', True);<br>&nbsp; end;<br>end;<br>
 
删除目录,不管是空目录还是非空目录:<br><br>function DoRemoveDir(sDirName:String):Boolean;<br>var<br>&nbsp; &nbsp;hFindFile:Cardinal;<br>&nbsp; &nbsp;tfile:String;<br>&nbsp; &nbsp;sCurDir:String;<br>&nbsp; &nbsp;bEmptyDir:Boolean;<br>&nbsp; &nbsp;FindFileData:WIN32_FIND_DATA;<br>begin<br>&nbsp; &nbsp;bEmptyDir:=True;<br>&nbsp; &nbsp;sCurDir:=GetCurrentDir;<br>&nbsp; &nbsp;SetLength(sCurDir,Length(sCurDir));<br>&nbsp; &nbsp;ChDir(sDirName);<br>&nbsp; &nbsp;hFindFile:=FindFirstFile('*.*',FindFileData);<br>&nbsp; &nbsp;if hFindFile&lt; &gt;INVALID_HANDLE_VALUE then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tfile:=FindFileData.cFileName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tfile='.') or (tfile='..') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bEmptyDir:=bEmptyDir and True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bEmptyDir:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FindFileData.dwFileAttributes=<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_ATTRIBUTE_DIRECTORY then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if sDirName[Length(sDirName)]&lt; &gt;'/' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoRemoveDir(sDirName+'/'+tfile)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoRemoveDir(sDirName+tfile);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not RemoveDirectory(PChar(tfile)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not DeleteFile(PChar(tfile)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; until FindNextFile(hFindFile,FindFileData)=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; FindClose(hFindFile);<br>&nbsp; &nbsp;end<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ChDir(sCurDir);<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;if bEmptyDir then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ChDir('..');<br>&nbsp; &nbsp; &nbsp; &nbsp; RemoveDirectory(PChar(sDirName));<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;ChDir(sCurDir);<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>function DeleteDir(sDirName:String):Boolean;<br>begin<br>&nbsp; &nbsp; &nbsp; if Length(sDirName)&lt; =0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);<br>end;<br>
 
多人接受答案了。
 
顶部