2012-04-27 14 views
10

私はC#アプリケーションを持っていて、そのファイルを整理するために、私はいくつかのDLLを "Data"というフォルダに持っています。私はEXEがDLLのためにこのフォルダをチェックするように、現在のディレクトリをチェックする方法と同じようにしたい。この情報を持つApp.Configを作成した場合は、C#app.configなしでプロービングprivatePathを設定しますか?

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <probing privatePath="Data" /> 
    </assemblyBinding> 
    </runtime> 
</configuration> 

問題なく動作します。私はApp.Configを持っていたくない。 app.configを使用せずにプロービングパスを設定する方法はありますか?

答えて

4

作成する新しいAppDomainsに対して行うことはできますが、現在の/既定のAppDomainの管理コードで行う方法はないと思います。

編集:プライベートパスでのAppDomainの作成:AppDomain.CreateDomainAppDomainSetup .PrivateBinPath

+0

+1。これは、AppDomainの作成時に設定する必要があります。 app.configは、プログラム起動時にランタイムから評価されます。 2番目のappdomainを作成するコード内の代替のみです。しかし、それは適切なアプローチかもしれません。 – TomTom

+0

どうすればいいですか? –

+0

リンクを使って解答を更新しました。 –

0

を使用setup.ApplicationBase = DATADIR。 `

` ``

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; 

:тожесамоеは、чтоиprivatePath = "データ"

[STAThread] 
    static void Main() 
    { 
     #region Add Dll Folder 

     System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(MainForm)); 
     string dataDir = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(new Uri(assembly.GetName().CodeBase).LocalPath), "Data"); 

     AppDomainSetup setup = new AppDomainSetup(); 
     setup.ApplicationBase = dataDir; 

     #endregion Add Dll Folder 

     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new MainForm()); 
    } 
3

は、これは古い質問ですが、あなたもそうのようなのAppDomain AssemblyResolveイベントを処理することができます``

と:

`` `

private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) 
    { 
     var probingPath = pathToYourDataFolderHere; 
     var assyName = new AssemblyName(args.Name); 

     var newPath = Path.Combine(probingPath, assyName.Name); 
     if (!newPath.EndsWith(".dll")) 
     { 
      newPath = newPath + ".dll"; 
     } 
     if (File.Exists(newPath)) 
     { 
      var assy = Assembly.LoadFile(newPath); 
      return assy; 
     } 
     return null; 
    } 

`` `