简体   繁体   English

vb.net求助,如何跳过未经许可删除文件

[英]vb.net help, how to skip deleting a file without permission

On VB.net i was making a file cleaner, one that deletes stuff like temp etc, however with folders like prefetch and temp where some are in use at the time is there a way to make the program skip over the undeletable files and clear everything else, thanks在 VB.net 上,我正在制作一个文件清理器,它可以删除 temp 等内容,但是对于 prefetch 和 temp 之类的文件夹,当时正在使用一些文件夹,有办法让程序跳过不可删除的文件并清除其他所有内容,谢谢

You should use the following code on Button click event (administrator required):您应该在按钮单击事件上使用以下代码(需要管理员):

Shell("CLEANMGR", "/d <drive_letter> /sagerun:64")

The following items may be deleted on execution of the above code:执行上述代码时,可能会删除以下项目:

  • Temporary Internet Files临时网络文件
  • Temporary Setup Files临时设置文件
  • Downloaded Program Files下载的程序文件
  • Old CHKDSK Files旧的 CHKDSK 文件
  • Recycle Bin回收站
  • Temporary Files临时文件
  • Windows DUMP and Error logs Windows DUMP 和错误日志

Source of using CleanMGR: cleanmgr command line switches...使用 CleanMGR 的来源: cleanmgr 命令行开关...

You need to wrap your delete routine in a try catch statement:您需要将删除例程包装在 try catch 语句中:

Try
  System.IO.File.Delete([FILENAME])
Catch ex as Exception
  'Log out to console
  Console.WriteLine(ex.Message)
End Try

If your goal is to write this utility (so you aren't interested in re-using an existing utility that does the same thing), there isn't a good way to check to see if you can delete a file before you try.如果您的目标是编写此实用程序(因此您对重新使用执行相同操作的现有实用程序不感兴趣),那么在尝试之前检查您是否可以删除文件并不是一个好方法。 Some things you can't test for, and even when you can, there's no guarantee that the file will be in the same state when you try to delete it (even a very short time later), so your operation might fail anyway.有些事情您无法测试,即使您可以测试,也无法保证当您尝试删除文件时(即使是很短的时间),该文件将位于同一个 state 中,因此您的操作可能无论如何都会失败。

Thus, the only way to go is to put a Try / Catch around every file operation (or accept the possibility that a failure might lead to a crash).因此,go 的唯一方法是在每个文件操作周围放置一个Try / Catch (或接受失败可能导致崩溃的可能性)。

The resulting pseudocode is something like:生成的伪代码类似于:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM