为默认 AppDomain 设置卷影复制的正确方法是什么 [英] What is the right way to set shadow copying for the default AppDomain
问题描述
关于我可以让默认的 AppDomain 使用某些程序集的卷影副本吗?,它描述了一个有效的激活解决方案在特定目录的默认 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";
不幸的是,这没有效果.那么问题来了,有没有办法改变当前appdomain的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<执行您的主应用程序/a> 方法.
- 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.
可以在 应用程序的影子复制 CodeProject 文章中找到一个好的起点.以下程序取自文章,稍作修改(未指定缓存路径:
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
替换为应用程序名称. - 将
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.
这篇关于为默认 AppDomain 设置卷影复制的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!