是否有可能做到这一点,即使在删除应用程序文件后,该应用程序一旦执行,仍将继续运行直至结束?
我有一个可移动的控制台应用程序,它在可移动驱动器上运行,并且即使驱动器被意外删除,也希望它继续运行,这可能吗?
我看到了http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory,但它似乎不起作用。
ElmoDev001的评论使我获得了想要的结果。
有错误,但是这里是基本思路:
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
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句