Write a program to delete itself after execution?

Showing Answers 1 - 2 of 2 Answers

kbjarnason

  • Jul 1st, 2010
 

There is no reliable way to do this in C.

First, if you use the conventional form of main - int main(int argc, char *argv[] ) - then argv[0] is supposed to hold the "program name" - but this can be emtpy, or could lack a path, or have any number of other differences from the actual filename you invoked.

Second, the system may not give permission for the program to delete this way.

Third, even if it does, on some systems, you cannot delete an executing program.

Or, in short, you can't get there from here.  One of several possible ways will work on some systems, no method will reliably work on all systems.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions