Solved

Delete File After Reboot Using VB .Net

Posted on 2006-10-24
9
2,927 Views
Last Modified: 2011-08-18
How do you set a file to delete after reboot in VB .Net?  I tried using MoveFileEx but couldn't get it to work.
0
Comment
Question by:marbro
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:ostdp
ID: 17800552
You can set up an entry in the registry under

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

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.

Example:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
"DeleteFile"="c:\\DeleteThisFile.Exe"
0
 
LVL 4

Expert Comment

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

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
"DeleteFile"="cmd.exe /C \"del c:\\yourfile.exe\""
0
 
LVL 5

Expert Comment

by:DoctorMahdi
ID: 17800652
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
System.IO.File.Delete(FileToDelete)
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

Regards
DoctorMahdi
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
ID: 17800702
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.
0
 

Author Comment

by:marbro
ID: 17804760
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?

thx,

Robert
0
 
LVL 4

Expert Comment

by:ostdp
ID: 17805098
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:

"c:\yourfile.txt="
0
 
LVL 4

Expert Comment

by:ostdp
ID: 17805107
0
 

Author Comment

by:marbro
ID: 17805147
ostdp, that didn't work :(

I found that the data syntax for replacing a file is something like this:
\??\c:\test2.xml
\??\c:\test.xml

Can't figure out how to do the delete.  I tried leaving the second line blank but didn't work.
0
 
LVL 22

Expert Comment

by:cookre
ID: 17808556
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.

E.g.:

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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about a problem I had getting the GPS LocationListener working.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question