Solved

Delete File After Reboot Using VB .Net

Posted on 2006-10-24
9
2,909 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:marbro
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:marbro
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
This is an explanation of a simple data model to help parse a JSON feed
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now