• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3028
  • Last Modified:

Delete File After Reboot Using VB .Net

How do you set a file to delete after reboot in VB .Net?  I tried using MoveFileEx but couldn't get it to work.
  • 4
  • 2
  • 2
  • +1
1 Solution
You can set up an entry in the registry under


which points to a program that will delete the file in question.

On the next reboot the program will get executed and the file deleted.


Windows Registry Editor Version 5.00

The easiest way would be by using the commandline interpreter like so:

"DeleteFile"="cmd.exe /C \"del c:\\yourfile.exe\""
Or without registry:

use the usual normal file deletion and put the code under form load event then write the program in excutable form and insert it in the startup list

Here is the code:
Dim FileToDelete As String
FileToDelete = "C:\test.txt"
If System.IO.File.Exists(FileToDelete) = True Then
MsgBox("File Deleted")
End If

when the windows restarts, your program run & the file will be deleted automatically

I agree with you it's not good way! but just a trick for those who don't like registry editing and it's hazards

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

If the target file gets used before RunOnce or Startup get called (for example, in a service) place an entry in

HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations

Otherwise, the file will be in use before the simple deletes are executed, and they will fail.
marbroAuthor Commented:
I need to delete the file before the system completely starts since the system locks it.  I think using the HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations would be best.   Can you give me the exact syntax/example in creating the proper PendingFileRenameOperations key to delete a file upon reboot?


For HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations the registry value consists of name/newname pairs of null-terminated strings, with a final extra null termination. Files to be deleted are indicated by a null newname.
The entry is of the REG_MULTI_SZ type and the oldname/newname pair as far as I know is delimited by "=".

try something like this:

marbroAuthor Commented:
ostdp, that didn't work :(

I found that the data syntax for replacing a file is something like this:

Can't figure out how to do the delete.  I tried leaving the second line blank but didn't work.
Sorry, In a rush I sometimes fail to add important info.

1) Create that value as multi-string

2) Enter line 1 as:
\??\c:\foo.bar or whatever

3) Enter line 2 as two nulls (the first is the end of that string and the second signifies the end of all entries
To do that, OK the first line, click on Modify Binary Data, then add four zeros (two nulls) at the end.

If you have multiple deletes, use just one null (00) on the intermediate even lines and two only at the end.


\0 (00 in the binary edit)
\??\c:\program files\long name\with spaces\but\no\quote.s

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now