C#设置探测privatePath而不用app.config?

我有一个C#应用程序,为了组织它的文件我在一个名为“Data”的文件夹中有一些DLL。 我希望EXE检查此文件夹中的DLL,就像它检查当前目录一样。 如果我使用以下信息创建了App.Config:

        

它没有问题。 我不想要App.Config。 有没有办法在不使用app.config的情况下设置探测路径?

您可以为您创建的新AppDomain执行此操作,我不相信有一种方法可以在当前/默认AppDomain的托管代码中执行此操作。

编辑:使用专用路径创建AppDomain:使用AppDomain.CreateDomain和AppDomainSetup .PrivateBinPath

你也可以像这样处理AppDomain AssemblyResolve事件:

 AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_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; } 

setup.ApplicationBase = dataDir; тожесамое,чтоиprivatePath=“Data”

  [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()); }