2014-11-05 31 views

回答

0

號:(

這個例子:

namespace ConsoleApplication 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var newDomain = AppDomain.CreateDomain("Alternative"); 

      Proxy proxyObj = (Proxy)newDomain.CreateInstanceAndUnwrap(typeof(Proxy).Assembly.GetName().FullName, 
                     typeof(Proxy).FullName); 

      Environment.SetEnvironmentVariable("HELLO_MSG", "Hello World", EnvironmentVariableTarget.Process); 

      proxyObj.ShowEnvironmentVariable(); 

      Console.ReadKey(); 
     } 
    } 

    class Proxy : MarshalByRefObject 
    { 
     public void ShowEnvironmentVariable() 
     { 
      var msg = Environment.GetEnvironmentVariable("HELLO_MSG"); 
      Console.WriteLine(String.Format("{0} (from '{1}' AppDomain)", msg, AppDomain.CurrentDomain.FriendlyName)); 
     } 
    } 
} 

將輸出:

Hello World (from 'Alternative' AppDomain) 

的過程是封裝環境變量的最具體的級別,AppDomain的意志仍然「住在裏面」的過程相同。

請注意,這將(如Directory.GetCurrentDirectory(),命令行參數等)

一種可能的解決方案是創建工作進程(從主進程產生的「.exe」應用程序),但這肯定會增加一些複雜的應用程序。

相關問題