我正在尝试修改 2 个 .exe 以从 1 个位置加载 DevExpress dll。
“产品”文件夹中的 .exe 文件与启动程序使用相同的 .dll 文件。我想避免将相同的 .dll 放入 Products 目录,而是从 1 个目录(启动器目录)读取 .exe。
我怎样才能实现这个目标?
最佳答案
您可以处理 AppDomain.AssemblyResolve 事件,并使用 Assembly.LoadFile 自行从目录加载程序集,为其尝试解析的程序集提供完整路径。
示例:
.
.
.
// elsewhere at app startup time attach the handler to the AppDomain.AssemblyResolve event
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
.
.
.
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName assemblyName = new AssemblyName(args.Name);
// this.ReadOnlyPaths is a List<string> of paths to search.
foreach (string path in this.ReadOnlyPaths)
{
// If specified assembly is located in the path, use it.
DirectoryInfo directoryInfo = new DirectoryInfo(path);
foreach (FileInfo fileInfo in directoryInfo.GetFiles())
{
string fileNameWithoutExt = fileInfo.Name.Replace(fileInfo.Extension, "");
if (assemblyName.Name.ToUpperInvariant() == fileNameWithoutExt.ToUpperInvariant())
{
return Assembly.Load(AssemblyName.GetAssemblyName(fileInfo.FullName));
}
}
}
return null;
}
关于c# - 更改 .NET 应用程序的程序集解析位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253734/