为默认AppDomain设置卷影复制的正确方法是什么

Blu*_*sky 9 .net shadow-copy appdomain appdomainsetup

有关我可以使默认AppDomain使用某些程序集的卷影副本吗?,它描述了一个工作解决方案,用于在特定目录的默认AppDomain中激活卷影复制.

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

AppDomain.CurrentDomain.SetShadowCopyPath(aDirectory);
AppDomain.CurrentDomain.SetShadowCopyFiles();
Run Code Online (Sandbox Code Playgroud)

但是因为这里使用的方法被标记为过时,我想知道现在正是什么方法来实现同样的目标.警告消息提示:

请调查AppDomainSetup.ShadowCopyDirectories的使用

AppDomain有一个这种类型的成员SetupInformation,可以带你进入这个简单的实现

AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories = aDirectory;
AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";
Run Code Online (Sandbox Code Playgroud)

不幸的是,这没有效果.所以问题是,有没有办法改变当前appdomain的AppDomainSetup以激活阴影复制?

Pan*_*nis 15

据我所知,这些方法仅适用于.NET Framework 1.1版.对于所有更高版本,您无法在主AppDomain上启用阴影复制.您需要创建一个新的AppDomain并适当地设置它.一种简单的方法是创建一个简单的加载器应用程序:

可以在Shadow Copying of Applications CodeProject文章中找到一个很好的起点.以下程序取自文章略作修改(未指定缓存路径:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你不得不:

  • 替换MyApplication.exe为可执行程序集的名称.
  • 替换MyApplication为apllication的名称.
  • 替换MyApplication.exe.config为应用程序配置文件的名称.如果您没有,那么您不需要设置它.