简体   繁体   English

C#如果删除应用程序文件后如何使应用程序继续运行

[英]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: 一种机制可能是:

  1. User starts app 用户启动应用
  2. App copies itself to the temp folder. 应用程序将自身复制到临时文件夹。
  3. App launches the temp copy using Process.Start and a /nocopy parameter App使用Process.Start和/ nocopy参数启动临时副本
  4. App closes itself. 应用关闭自己。
  5. Temp app starts up, reads the /nocopy parameter and skips the copy and launches. 临时应用程序启动,读取/ nocopy参数并跳过副本并启动。

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.

相关问题 关闭应用程序C#后,过程继续 - Process continue after closing application c# c#/core 关闭应用程序但允许子进程继续运行 - c#/core close application but allow a child process to continue running Windows注销后如何继续运行C#应用程序 - how to keep running C# application after windows log off 如何在不创建安装文件的情况下运行 c# 应用程序? - how to running c# application without create setup file? 如何跳过csv文件中的一些空行并继续读取包含数据的行? C#控制台应用程序 - How to skip some empty rows in csv file and continue reading rows with data? c# console application 在C#应用程序中运行CPP文件? - Running a CPP file in a C# application? 即使成功构建项目,应用程序也无法与MDF文件建立连接[Visual Studio 2010 C#] - The application cannot make a connection with MDF file even building the project successfully [Visual Studio 2010 C#] 如何制作包含C#应用程序和sql server的安装文件 - How To make setup file that contains C# application and sql server C# 关闭应用程序后如何重命名当前文件名 - C# How to rename current file name after closing the application 如何在 C# 中删除正在运行的应用程序? - How to delete a running application in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM