Solved

Delete File After Reboot Using VB .Net

Posted on 2006-10-24
9
2,954 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
Progress

628 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