Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete File After Reboot Using VB .Net

Posted on 2006-10-24
9
Medium Priority
?
2,982 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Accepted Solution

by:
cookre earned 1500 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

721 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