What I'm trying to do is once the user closes my program, I want the program to automatically delete the exe file. This is what I have but when I close the program I get an error that the path is not accessible, and I assume it's because at the second it is being used and can't delete.
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
string path = Application.ExecutablePath;
File.Delete(path);
}
If anyone knows how to either execute the code once the program is fully closed or how to modify this code to make it do that, I'd appreciate it a lot.
You could execute a powershell command within C# to delete the file for you:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Remove-Item param($param1)");
PowerShellInstace.AddParameter("param1", Application.ExecutablePath)
}
}
https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/