I am currently searching for a way to delete a file after my C# .NET program exits. I am aware that there is AppDomain.ProcessExit, but due to this blog post I cannot use it because I need a guaranteed event. I am open to any solution.
Asked
Active
Viewed 66 times
0
-
What type of application is it? – Mike G Apr 06 '18 at 13:57
-
Delete it when you start the program. – LarsTech Apr 06 '18 at 13:57
-
This feels like a XY Problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Why do you need to delete the file? – mjwills Apr 06 '18 at 13:58
-
what type of c# .net app is it? console? service? wpf? winforms? etc...? – Nikola.Lukovic Apr 06 '18 at 13:58
-
https://stackoverflow.com/a/4657392/34092 would be one approach to consider. When the child process dies then the parent likely just died. It isn't bulletproof though (since you could terminate the child **then** the parent). – mjwills Apr 06 '18 at 14:03
-
3There will be some things you just can't guard against. The computer losing power, for example. Make sure that whatever process you require this functionality for can handle recovering from the file not getting deleted if something exceptional happens. – Bradley Uffner Apr 06 '18 at 14:05
-
1The best solution would likely be to create a service which monitors the state of your application. When your application is not running and if the file exists just delete it. The service will then also start automatically (if configured correctly) so on windows start up it should handle the situation fine. – TheLethalCoder Apr 06 '18 at 14:10
-
It is currently a console application, but in future it could also be used as a service, web application, etc. My company loves reusing so the algorithm stays the same, but the implementations are diffrent. I am already doing a cleanup at the start of my application. I need to delete the file because it contains sensetive data (customer data). – school_guy Apr 06 '18 at 16:38
-
A service seems like a lot of work for a program which is in the shell basically a two liner :D I was hoping for something smaller... – school_guy Apr 06 '18 at 16:39
-
Sensitive data shouldn't be sitting in a file. – LarsTech Apr 06 '18 at 16:42
-
It has to be because the program which takes the file as input does not support a pipe. – school_guy Apr 06 '18 at 17:06
-
`I was hoping for something smaller...` What you are effectively asking for is something like `Is there a way for God to tell me 2 seconds before I die that I am going to die?` No, there isn't a way except for the variety of hacks we have mentioned. If someone terminates your program _rudely_ they aren't interested in it cleaning itself up. They want it dead - **now**. – mjwills Apr 06 '18 at 21:13
-
`as input does not support a pipe.` Please update your problem with **all** of the constraints. This is a XY Problem. – mjwills Apr 06 '18 at 21:14
-
@mjwills I guess you are right. If you package your last two comments as an answer, I will accept it.... – school_guy Apr 07 '18 at 14:04