2011-07-20 222 views
5

我使用此代碼刪除整個目錄:永久刪除目錄

uses 
    ShellApi; 

function DelDir(dir: string): Boolean; 
var 
    fos: TSHFileOpStruct; 
begin 
    ZeroMemory(@fos, SizeOf(fos)); 
    with fos do 
    begin 
    wFunc := FO_DELETE; 
    fFlags := FOF_SILENT or FOF_NOCONFIRMATION; 
    pFrom := PChar(dir + #0); 
    end; 
    Result := (0 = ShFileOperation(fos)); 
end; 

是否有任何標誌,我可以設置爲允許永久移除刪除的目錄中?
通過永久刪除,我的意思是它不會在刪除後顯示在回收站中,因爲這是使用DelDir函數時會發生的情況。

+2

什麼是permament去除?所以它不能被重新創建? – Jacob

+0

對不起。通過永久刪除,我的意思是它不會顯示在回收站中。 – ple103

+0

更新了原始帖子。 – ple103

回答

4

嘗試設置

FileOpStruct.pTo := nil;

例子:

function DeleteTree(const APath: String): Boolean; 
var 
    FileOpStruct : TShFileOpStruct; 
    ErrorCode: Integer; 
begin 
    Result := False; 
    if DirectoryExists(APath) then begin 
    FillChar(FileOpStruct, SizeOf(FileOpStruct), #0); 
    FileOpStruct.Wnd := 0; 
    FileOpStruct.wFunc := FO_DELETE; 
    FileOpStruct.pFrom := PChar(APath + #0#0); 
    FileOpStruct.pTo := nil; 
    FileOpStruct.fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_NOCONFIRMMKDIR; 
    FileOpStruct.lpszProgressTitle := nil; 
    ErrorCode := ShFileOperation(FileOpStruct); 
    Result := ErrorCode = 0; 
    end; 
end; 
+0

我認爲用兩個空終止符('pFrom')就足夠了。 –

+2

你的建議改變什麼都不做。原始代碼已經使用ZeroMemory來設置所有的字段,這有效地將pTo設置爲零。原始代碼中有什麼問題,以及您的問題如何解決? –