[英]C# How to make application continue running even after application file is deleted
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. 我看到http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory但它似乎不起作用。
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: 您必须在main方法或引导程序/启动中的某个位置加载所有相关程序集:
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: 一种机制可能是:
The comment by ElmoDev001 led me to achieve the results I wanted. ElmoDev001的评论让我达到了我想要的结果。
There are errors but here is the general idea: 有错误,但这是一般的想法:
Program.cs (Main class) Program.cs(主类)
class Program {
static void Main(string[] args)
{
String Temp = System.IO.Path.GetTempPath();
String tmpDir = Temp + @"tmp\";
String fileName = String.Concat(Process.GetCurrentProcess().ProcessName, ".exe");
String filePath = Path.Combine(Extension.AssemblyDirectory, fileName);
String tempFilePath = Path.Combine(tmpDir, fileName);
if (!Directory.Exists(tmpDir)) { Directory.CreateDirectory(tmpDir); }
if (!(string.Equals(filePath, tempFilePath))) // if the application is not running at temp folder
{
if (!(File.Exists(tempFilePath))) // if the application does not exist at temp folder
{
ext.initFile();
}
else if (File.Exists(tempFilePath)) // if the application already exists at temp folder
{
File.Delete(tempFilePath);
ext.initFile();
}
}
else if (string.Equals(filePath, tempFilePath)) // if the application is running at temp folder
{
//main code
}
}
}
Extension.cs Extension.cs
class Extension
{
String Temp = System.IO.Path.GetTempPath();
String tmpDir = Temp + @"tmp\";
String fileName = String.Concat(Process.GetCurrentProcess().ProcessName, ".exe");
String filePath = Path.Combine(Extension.AssemblyDirectory, fileName);
String tempFilePath = Path.Combine(tmpDir, fileName);
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
public static void initFile()
{
File.Copy(filePath, tempFilePath);
Process.Start(tempFilePath);
System.Environment.Exit(0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.