![](/img/trans.png)
[英]How to delete all text in a text file on ftp server without deleting file itself in vb.net
[英]vb.net help, how to skip deleting a file without permission
在 VB.net 上,我正在制作一個文件清理器,它可以刪除 temp 等內容,但是對於 prefetch 和 temp 之類的文件夾,當時正在使用一些文件夾,有辦法讓程序跳過不可刪除的文件並清除其他所有內容,謝謝
您應該在按鈕單擊事件上使用以下代碼(需要管理員):
Shell("CLEANMGR", "/d <drive_letter> /sagerun:64")
執行上述代碼時,可能會刪除以下項目:
使用 CleanMGR 的來源: cleanmgr 命令行開關...
您需要將刪除例程包裝在 try catch 語句中:
Try
System.IO.File.Delete([FILENAME])
Catch ex as Exception
'Log out to console
Console.WriteLine(ex.Message)
End Try
如果您的目標是編寫此實用程序(因此您對重新使用執行相同操作的現有實用程序不感興趣),那么在嘗試之前檢查您是否可以刪除文件並不是一個好方法。 有些事情您無法測試,即使您可以測試,也無法保證當您嘗試刪除文件時(即使是很短的時間),該文件將位於同一個 state 中,因此您的操作可能無論如何都會失敗。
因此,go 的唯一方法是在每個文件操作周圍放置一個Try
/ Catch
(或接受失敗可能導致崩潰的可能性)。
生成的偽代碼類似於:
For Each (item in top-level list of places to search)
Try
'If directory, try to enter it. If successful, recurse.
'If file, try to delete it.
Catch
'Can log or just skip, this is a valid case to eat an exception
'At a minimum, you might see a System.IO.IOException or a couple of different security-related exceptions
End Try
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.