Is it possible to make it so that even after the application file is removed, the application once executed, will still continue running until the end?
I have a portable Console Application that runs on a removable drive and would like it to continue running even if the drive is accidentally removed, is that possible?
I saw http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory but it does not seem to work.
If your console application has some referenced assemblies then they might not be loaded until they are used.
You have to load all related assemblies in your main method or somewhere in the bootstrapper/startup:
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
The problem with the approach in the article (from your perspective) is that you need to launch it from another application, and that application's binary must be present at all times, so you can't launch that one from the same location, or you haven't got rid of the problem.
One mechanism could be:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With