Solved

Delete File After Reboot Using VB .Net

Posted on 2006-10-24
9
2,924 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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