与Can I make the default AppDomain use shadow copies of certain assemblies?相关,它描述了一个工作解决方案,用于在特定目录的默认AppDomain中激活卷影复制。
基本上它说要使用这些简单的方法:
AppDomain.CurrentDomain.SetShadowCopyPath(aDirectory);
AppDomain.CurrentDomain.SetShadowCopyFiles();
但是因为这里使用的方法被标记为过时,我想知道现在正是什么方法来实现同样的目标。警告消息提示:
请调查AppDomainSetup.ShadowCopyDirectories的使用
AppDomain有一个名为SetupInformation
的此类成员,它可能会带您进行这种简单的实现
AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories = aDirectory;
AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";
不幸的是,这没有任何效果。 所以问题是,有没有办法改变当前appdomain的AppDomainSetup来激活阴影复制?
答案 0 :(得分: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);
}
}
}
你必须:
MyApplication.exe
替换为可执行程序集的名称。MyApplication
替换为apllication的名称。MyApplication.exe.config
替换为应用程序配置文件的名称。如果您没有,那么您不需要设置它。答案 1 :(得分:0)
您不需要创建单独的应用程序。您可以在主方法中生成子域,也可以根据AppDomain.CurrentDomain.IsDefaultAppDomain()
值调用实际的主方法:
public static void Main(string[] args)
{
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
// Loader
var entryPoint = System.Reflection.Assembly
.GetExecutingAssembly();
var applicationName = entryPoint.GetName().Name;
// Create the setup for the new domain:
var setup = new AppDomainSetup();
setup.ApplicationName = applicationName;
setup.ShadowCopyFiles = "true"; // note: it isn't a bool
// Create the application domain. The evidence of this
// running assembly is used for the new domain:
AppDomain domain = AppDomain.CreateDomain(
applicationName,
AppDomain.CurrentDomain.Evidence,
setup);
try
{
// Start MyApplication by executing the assembly:
domain.ExecuteAssembly(entryPoint.Location, args);
}
finally
{
// After the MyApplication has finished clean up:
AppDomain.Unload(domain);
}
}
else
{
// Main
ActualMain(args);
}
}
public static int ActualMain(string[] args)
{
//Hello-world!
}