使用AppDomain进行卷影复制以在运行时覆盖exe

时间:2014-03-14 17:36:20

标签: c# appdomain shadow-copy

在以下示例应用程序中,我创建了一个新的AppDomain,并在启用了卷影复制的情况下执行它。从新AppDomain我然后尝试删除(替换)原始主exe。但是我得到了一个"访问被拒绝错误"。有趣的是,在启动程序后,从Windows资源管理器中可以重命名主exe(但不能删除它)。

阴影复制是否可以用于运行时覆盖主exe?

static void Main(string[] args)
{
    // enable comments if you wanna try to overwrite the original exe (with a 
    // copy of itself made in the default AppDomain) instead of deleting it

    if (AppDomain.CurrentDomain.IsDefaultAppDomain())
    {
        Console.WriteLine("I'm the default domain");
        System.Reflection.Assembly currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
        string startupPath = currentAssembly.Location;

        //if (!File.Exists(startupPath + ".copy"))
        //    File.Copy(startupPath, startupPath + ".copy");

        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationName = Path.GetFileName(startupPath);
        setup.ShadowCopyFiles = "true";

        AppDomain domain = AppDomain.CreateDomain(setup.ApplicationName, AppDomain.CurrentDomain.Evidence, setup);
        domain.SetData("APPPATH", startupPath);

        domain.ExecuteAssembly(setup.ApplicationName, args);

        return;
    }

    Console.WriteLine("I'm the created domain");
    Console.WriteLine("Replacing main exe. Press any key to continue");
    Console.ReadLine();

    string mainExePath = (string)AppDomain.CurrentDomain.GetData("APPPATH");
    //string copyPath = mainExePath + ".copy";
    try
    {
        File.Delete(mainExePath );
        //File.Copy(copyPath, mainExePath );
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error! " + ex.Message);
        Console.ReadLine();
        return;
    }

    Console.WriteLine("Succesfull!");
    Console.ReadLine();
}

3 个答案:

答案 0 :(得分:10)

您可以在具有多个AppDomain的单个应用程序中实现自我更新应用程序。诀窍是将应用程序可执行文件移动到临时目录并复制回您的目录,然后将复制的可执行文件加载到新的AppDomain中。

static class Program
{
    private const string DELETED_FILES_SUBFOLDER = "__delete";

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
    [STAThread]
    static int Main()
    {
        // Check if shadow copying is already enabled
        if (AppDomain.CurrentDomain.IsDefaultAppDomain())
        {
            // Get the startup path.
            string assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string assemblyDirectory = Path.GetDirectoryName(assemblyPath);
            string assemblyFile = Path.GetFileName(assemblyPath);

            // Check deleted files folders existance
            string deletionDirectory = Path.Combine(assemblyDirectory, DELETED_FILES_SUBFOLDER);
            if (Directory.Exists(deletionDirectory))
            {
                // Delete old files from this folder
                foreach (var oldFile in Directory.EnumerateFiles(deletionDirectory, String.Format("{0}_*{1}", Path.GetFileNameWithoutExtension(assemblyFile), Path.GetExtension(assemblyFile))))
                {
                    File.Delete(Path.Combine(deletionDirectory, oldFile));
                }
            }
            else
            {
                Directory.CreateDirectory(deletionDirectory);
            }
            // Move the current assembly to the deletion folder.
            string movedFileName = String.Format("{0}_{1:yyyyMMddHHmmss}{2}", Path.GetFileNameWithoutExtension(assemblyFile), DateTime.Now, Path.GetExtension(assemblyFile));
            string movedFilePath = Path.Combine(assemblyDirectory, DELETED_FILES_SUBFOLDER, movedFileName);
            File.Move(assemblyPath, movedFilePath);
            // Copy the file back
            File.Copy(movedFilePath, assemblyPath);

            bool reload = true;
            while (reload)
            {
                // Create the setup for the new domain
                AppDomainSetup setup = new AppDomainSetup();
                setup.ApplicationName = assemblyFile;
                setup.ShadowCopyFiles = true.ToString().ToLowerInvariant();

                // Create an application domain. Run 
                AppDomain domain = AppDomain.CreateDomain(setup.ApplicationName, AppDomain.CurrentDomain.Evidence, setup);

                // Start application by executing the assembly.
                int exitCode = domain.ExecuteAssembly(setup.ApplicationName);
                reload = !(exitCode == 0);
                AppDomain.Unload(domain);
            }
            return 2;
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainForm mainForm = new MainForm();
            Application.Run(mainForm);
            return mainForm.ExitCode;
        }
    }
}

答案 1 :(得分:0)

由于它是MEF的一个有趣的用例,我已经快速演示了如何在C#中热插拔运行代码。这非常简单,并且遗漏了许多边缘情况。

https://github.com/i-e-b/MefExperiments

值得注意的课程:

  • src/PluginWatcher/PluginWatcher.cs - 查看新合同实施的文件夹
  • src/HotSwap.Contracts/IHotSwap.cs - 热插拔的最低基本合同
  • src/HotSwapDemo.App/Program.cs - 实时代码交换

这不会将任务.dll锁定在Plugins文件夹中,因此您可以在部署新版本后删除旧版本。 希望有所帮助。

答案 2 :(得分:0)

您特别要求在更新过程中使用ShadowCopy。如果那不是一个固定的要求(为什么会这样?),那么这些对我来说真的是大开眼界:

https://visualstudiomagazine.com/articles/2017/12/15/replace-running-app.aspx

https://www.codeproject.com/Articles/731954/Simple-Auto-Update-Let-your-application-update-i

这取决于您重命名目标文件(即使由于其正在运行而被锁定,也可以重命名),然后将所需文件移动/复制到现在释放的目标位置。

vs-magazine杂志上的文章非常详细,其中包括一些巧妙的技巧,例如找出当前应用程序是否正在使用文件(尽管仅适用于exe,.dll和其他,但必须提出解决方案)。 / p>