Is spontaneous deletion of C # .exe possible?

I created a basic Windows Form application. I want to make my program uninstall after the date I selected.

In particular, when someone clicks on .exe to start it, and if it is after a certain date, .exe will be deleted. Is it possible? If so, how do I do this?

I think my code would look something like this:

DateTime expiration = new DateTime(2013, 10, 31) //Oct. 31, 2013 If (DateTime.Today > expiration) { //command to self-delete } else { //proceed normally } 
+6
source share
7 answers

You must make sure that your application is already closed when you want to delete the file. I would suggest something similar to the following: you will need some changes.

The following example works with windows and needs to be modified for other operating systems.

 /// <summary> /// Represents the entry point of our application. /// </summary> /// <param name="args">Possibly spcified command line arguments.</param> public static void Main(string[] args) { string batchCommands = string.Empty; string exeFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///",string.Empty).Replace("/","\\"); batchCommands += "@ECHO OFF\n"; // Do not show any output batchCommands += "ping 127.0.0.1 > nul\n"; // Wait approximately 4 seconds (so that the process is already terminated) batchCommands += "echo j | del /F "; // Delete the executeable batchCommands += exeFileName + "\n"; batchCommands += "echo j | del deleteMyProgram.bat"; // Delete this bat file File.WriteAllText("deleteMyProgram.bat", batchCommands); Process.Start("deleteMyProgram.bat"); } 
+7
source

It works, a command line operation is performed to remove itself.

 Process.Start( new ProcessStartInfo() { Arguments = "/C choice /CY /N /DY /T 3 & Del \"" + Application.ExecutablePath +"\"", WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, FileName = "cmd.exe" }); 
+8
source

He cannot delete himself during his launch, because his executable files and libraries will be locked. You can write a second program that takes a process identifier as an argument, waits for the process to die, and then deletes the first program. Paste this as a resource into the main application, then extract it to %TEMP% , start and exit.

This method is commonly used in conjunction with automatic updates, and it is definitely not proof of reliability.

+3
source

It is impossible for the program to delete . Think about it in terms of taskmgr.

you see myprogram.exe running, and myprogram.exe trying to remove myprogram.exe . Can you think of a problem? I can .. it's already in use.

This is why we see that the installation programs are not uninstalled.

+2
source

Yes. For example, run a temporary batch job to remove exe after it is completed. However, you cannot be sure that your exe is really deleted.

You cannot uninstall exe while it starts.

This will not work:

 System.IO.File.Delete(System.AppDomain.CurrentDomain.FriendlyName); 
+1
source
 void Form1Load(object sender, System.EventArgs e) { if (System.IO.File.Exists("C:/myfiles.exe")) { if (System.IO.File.Exists("C:/text.txt")) { read c:/text.txt and System.IO.File.Delete(" read data ") and System.IO.File.Delete("c:/text.txt") } } else { string exePath = Application.ExecutablePath; System.IO.File.Copy(exePath, @"C:/myfiles.exe"); and create c:/text.txt in write.... " exepath " and c:/myfiles.exe run code Application.Exit(); } } 
0
source

Try something like this:

 using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; //Include a reference to system class MyClass { static void Main() { InitiateSelfDestructSequence(); Thread.Sleep(10000); } static void InitiateSelfDestructSequence() { string batchScriptName = "BatchScript.bat"; using (StreamWriter writer = File.AppendText(batchScriptName)) { writer.WriteLine(":Loop"); writer.WriteLine("Tasklist /fi \"PID eq " + Process.GetCurrentProcess().Id.ToString() + "\" | find \":\""); writer.WriteLine("if Errorlevel 1 ("); writer.WriteLine(" Timeout /T 1 /Nobreak"); writer.WriteLine(" Goto Loop"); writer.WriteLine(")"); writer.WriteLine("Del \"" + (new FileInfo((new Uri(Assembly.GetExecutingAssembly().CodeBase)).LocalPath)).Name + "\""); } Process.Start(new ProcessStartInfo() { Arguments = "/C " + batchScriptName + " & Del " + batchScriptName, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, FileName = "cmd.exe" }); } } 
-1
source

Source: https://habr.com/ru/post/957058/


All Articles