How to implement a self-delete exe after running once ?

K

Kid

Hi

I want to write some exe which only runs once :

(1) How can I delete it after running once ?

(2) If the exe is locked, can I delete it when next Windows restarting , is
there
some system temp folder can meet this requirement ?

Thank you .
 
S

SvenC

Hi Kid,
I want to write some exe which only runs once :

(1) How can I delete it after running once ?

I didn't try it myself but you might try using CreateFile to open your
exe with FILE_FLAG_DELETE_ON_CLOSE.
(2) If the exe is locked, can I delete it when next Windows restarting ,
is
there
some system temp folder can meet this requirement ?

http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx
MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT and NULL as destination
 
D

David Ching

Ian Semmel said:
Whenever I read questions like this I start to think "malicious".

Uninstall apps need to delete themselves and the folder they are running
from. I do it all the time.

-- David
 
D

David Ching

Alex Blekhman said:

Thanks Alex! An excellent article. I've used the self-deleting batch file
method, which is reliable but requires creating a file on disk. The last
technique presented which starts a well-known process like Explorer and
injects code into it... well I've done something like that too. It's spiffy
and works, but also requires Explorer.exe to be present (I guess that most
systems will have that.)

-- David
 
M

Michael B. Trausch

Hi Kid,


I didn't try it myself but you might try using CreateFile to open your
exe with FILE_FLAG_DELETE_ON_CLOSE.


http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx
MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT and NULL as destination

[Removed followups to groups where this would not apply.]

Just a side note---if your application intends to be portable for any
reason or will become portable, be sure to check
System.Environment.OSVersion.Platform before doing this. Under UNIX
and UNIX-like systems, you can just delete the file while the process
is running:

/*
* self-delete-unix.cs - Delete oneself and then run.
*/
using System;
using System.IO;
using System.Threading;

public class Program {
public static int Main(string[] args) {
string progfile = "self-delete-unix.exe";
File.Delete(progfile);

Hi();
Thread.Sleep(10000);
Hi2();
return(0);
}

public static void Hi() {
Console.WriteLine("I have been deleted.");
}

public static void Hi2() {
Console.WriteLine("I have been deleted.");
}
}

This works because on UNIX systems, files will be removed from
directory listings, but won't actually be deleted until there are no
more processes holding then open. Once the last file descriptor
referring to the file is closed, the operating system will finalize
removal of the file; before then, it will appear as if deleted (e.g.,
be detached from the directory and so forth, and no new processes will
be able to open it). Once closed, all that is left to do is free the
blocks the file occupied.

Also, keep in mind for portable applications that if you delete a file
successfully, that doesn't mean you closed all open refs (on POSIX
systems anyway). Just an FYI.

--- Mike
 
C

Cor Ligthert[MVP]

Kid,

Use the Mutex solution for this (there are plenty of examples on Internet)

Cor
 
C

Cor Ligthert[MVP]

Sorry,

I have read it to quick, I thought it was about an exe running once

Cor
 
D

Dean Earley

David said:
Uninstall apps need to delete themselves and the folder they are running
from. I do it all the time.

That's what the MoveFileEx() method is for.

Having said that, this is fine for auto created files. Deleting the file
that the user has downloaded and put somewhere would come under
malicious, as it isnt their file to delete.

What if it's put on a CD/read only medium?
 
D

David Ching

Dean Earley said:
That's what the MoveFileEx() method is for.

I understand MoveFileEx() does not delete the file until reboot. Users like
to see that the files have gone when the Uninstall has completed. And since
they hate rebooting, I don't find MoveFileEx() to fit the bill.

Having said that, this is fine for auto created files. Deleting the file
that the user has downloaded and put somewhere would come under malicious,
as it isnt their file to delete.

What if it's put on a CD/read only medium?

Agreed.

-- David
 
B

Bruce Eitman [eMVP]

It appears to me that you blasted this question to as many newsgroups as you
could think of. Only one of them deals with Windows CE. So you have
receive a lot of replys from what looks like BIG Windows programmers.

Maybe if you tell us what your target is and more details about your
requirements you will get more valuable replys, if the ones that you have
received don't solve your problem.

--
Bruce Eitman (eMVP)
Senior Engineer
Bruce.Eitman AT EuroTech DOT com
My BLOG http://geekswithblogs.net/bruceeitman

EuroTech Inc.
www.EuroTech.com
 
R

r_z_aret

Hi

I want to write some exe which only runs once :

(1) How can I delete it after running once ?

My 15 Apr 2004 contribution to a thread called "
Self Deleting EXE and DELETE_ON_CLOSE flag " in
comp.os.ms-windows.programmer.win32 includes code for a function I
use. I've tested it under several versions of "big" Windows. It won't
even compile for Windows CE
(2) If the exe is locked, can I delete it when next Windows restarting , is
there
some system temp folder can meet this requirement ?

Thank you .

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top