什么是正确的方式来设置阴影复制默认的AppDomain阴影、正确、方式、AppDomain

2023-09-03 02:55:24 作者:将你推入深渊

有关我可以做出一定的程序集的默认应用程序域使用卷影副本?,它描述了一个可行的解决方案,以激活默认的AppDomain特定目录中的影子复制。

Relating to Can I make the default AppDomain use shadow copies of certain assemblies?, it describes a working solution to activate shadow copying within the default AppDomain for a specific directory.

基本上它说,使用这些简单的方法:

Basically it says to use these simple methods:

AppDomain.CurrentDomain.SetShadowCopyPath(aDirectory);
AppDomain.CurrentDomain.SetShadowCopyFiles();

但由于这里使用的方法被标记为过时我不知道什么是现在完成同样的正确方法。警告信息提示为:

But because the methods used here are marked as obsolete I was wondering what is now the correct way to accomplish the same. The warning message hints to:

请调查使用AppDomainSetup.ShadowCopyDirectories而不是

Please investigate the use of AppDomainSetup.ShadowCopyDirectories instead

一个AppDomain有这种类型的所谓的成员 SetupInformation 这可能会为你带来这种简单的实现

An AppDomain has a member of this type called SetupInformation which might bring you to this straightforward implementation

AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories = aDirectory;
AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";

不幸的是这没有任何影响。 所以,问题是,有没有办法改变目前的应用程序域的AppDomainSetup激活阴影复制?

Unfortunately this has no effect. So the question is, is there a way to alter the AppDomainSetup of the current appdomain to activate shadow copying ?

推荐答案

据我所知,这些方法只能在.NET Framework 1.1版。对于所有以后的版本,你不能启用主AppDomain的阴影复制。您需要创建一个新的的AppDomain 并设置它恰如其分。一个简单的方法是创建一个加载应用程序,简单地说:

As far as I know these methods only work on .NET Framework version 1.1. For all later versions you cannot enable shadow-copying on the main AppDomain. You need to create a new AppDomain and set-it up appropriately. A simple approach is to create a loader application that simply:

创建一个新的AppDomain与影复制启用。对于这一点,你将不得不使用的AppDomain.CreateDomain这需要一个 AppDomainSetup 的参数。 执行主应用程序使用AppDomain.ExecuteAssembly方法 Creates a new AppDomain with shadow-copy enabled. For this you will have to use one of the overloads of AppDomain.CreateDomain that take an AppDomainSetup parameter. Executes your main application using the AppDomain.ExecuteAssembly method.

有一个良好的起点,可以在应用程序影复制找到 $ C $的CProject文章。以下的程序是取自文章有轻微的变形(未指定的高速缓存路径:

A good starting point can be found in the Shadow Copying of Applications CodeProject article. The following program is taken from the article with a slight modification (the cache path is not specified:

using System;
using System.IO;

namespace Loader
{
    static class Program
    {
        [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
        [STAThread]
        static void Main()
        {
            /* Enable shadow copying */

            // Get the startup path. Both assemblies (Loader and
            // MyApplication) reside in the same directory:
            string startupPath = Path.GetDirectoryName(
                System.Reflection.Assembly
                .GetExecutingAssembly().Location);

            string configFile = Path.Combine(
                startupPath,
                "MyApplication.exe.config");
            string assembly = Path.Combine(
                startupPath,
                "MyApplication.exe");

            // Create the setup for the new domain:
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationName = "MyApplication";
            setup.ShadowCopyFiles = "true"; // note: it isn't a bool
            setup.ConfigurationFile = configFile;

            // Create the application domain. The evidence of this
            // running assembly is used for the new domain:
            AppDomain domain = AppDomain.CreateDomain(
                "MyApplication",
                AppDomain.CurrentDomain.Evidence,
                setup);

            // Start MyApplication by executing the assembly:
            domain.ExecuteAssembly(assembly);

            // After the MyApplication has finished clean up:
            AppDomain.Unload(domain);
        }
    }
}

您必须:

替换 MyApplication.exe 与你的可执行程序集的名称。 替换所有MyApplication 与apllication的名称。 替换 MyApplication.exe.config 与您的应用程序的配置文件的名称。如果你没有一个那么你就需要设置这个。 Replace MyApplication.exe with the name of your executable assembly. Replace MyApplication with the name of apllication. Replace MyApplication.exe.config with the name of you application's configuration file. If you do not have one then you do not need to set this.