2012-09-20 26 views
4

有關Can I make the default AppDomain use shadow copies of certain assemblies?,它描述了一個工作解決方案來激活特定目錄的默認AppDomain內的影子複製。什麼是爲默認AppDomain設置影子複製的正確方法

基本上它說,使用這些簡單的方法:

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

但由於這裏使用的方法被標記爲過時我不知道現在是什麼來完成相同的正確方法。警告信息提示來:

請調查使用AppDomainSetup.ShadowCopyDirectories代替

一個AppDomain擁有這種類型稱爲SetupInformation的成員,這可能會爲你帶來這種簡單的實現

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

不幸的是,這沒有效果。 所以問題是,有沒有辦法改變當前appdomain的AppDomainSetup來激活陰影複製?

+1

要回答_「不幸的是,這沒有效果。 「_(詢問3年後,我知道),這是因爲'SetupInformation'屬性創建了一個_Clone_內部'FusionStore'屬性,該屬性反過來是對用於初始化當前域的實際'AppDomainSetup'的引用。想法是,初始化後,這些屬性不能再被修改(雖然我想知道他們爲什麼沒有使它們成爲gettor,只是爲了表示這一點)。 – Abel

回答

13

據我所知,這些方法只適用於.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替換爲應用程序的名稱
  • MyApplication.exe.config替換爲應用程序配置文件的名稱如果您沒有該名稱,則不需要設置它
相關問題