简体   繁体   中英

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

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
  • Recycle Bin
  • Temporary Files
  • Windows DUMP and Error logs

Source of using CleanMGR: cleanmgr command line switches...

You need to wrap your delete routine in a try catch statement:

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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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