如何为应用程序设置 C# 库路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set C# library path for an application?
提问by
I have C# application that uses a dll. When I try to run the application, it can't find the dll, unless it is in the same directory or in GAC. I do not want to have it in the same directory and I do not want to install it to GAC. Is there any way how to tell the application where to look for the library? (For example if I want to distribute the application to customers and they want to use their own applications that would use the dll.)
我有一个使用 dll 的 C# 应用程序。当我尝试运行该应用程序时,它找不到 dll,除非它在同一目录或 GAC 中。我不想将它放在同一目录中,也不想将其安装到 GAC。有什么方法可以告诉应用程序在哪里寻找图书馆?(例如,如果我想将应用程序分发给客户,而他们想使用自己的应用程序,这些应用程序会使用 dll。)
Added:
添加:
I would like to have this file structure:
我想要这个文件结构:
MainFolder: Libraries, Applications
主文件夹:库、应用程序
Libraries: lib.dll
库:lib.dll
Applications: app1.exe
应用程序:app1.exe
I don't want to copy it to GAC or have lib.dll in folder Applications. Is it possible?
我不想将它复制到 GAC 或在文件夹应用程序中有 lib.dll。是否可以?
回答by Mehrdad Afshari
回答by Gerrie Schenck
Like I said in my answer on your previous question:
就像我在上一个问题的回答中所说的那样:
Use Assembly Redirectioninstructions in your app.config or machine.config.
在 app.config 或 machine.config 中使用程序集重定向说明。
回答by GvS
回答by Jon Skeet
The DLL will have to either be in the GAC or in the application's directory or a subdirectory, as the answers to your earlier question said.
DLL 必须位于 GAC 或应用程序的目录或子目录中,如您之前问题的答案所述。
If your customers want to write their own applications using the DLL, you should either install it in the GAC or get them to copy the DLL too. Having multiple copies of the library doesn't soundlike a good thing, but it really is: it means you can upgrade one copy to a different version without breaking everything else.
如果您的客户想要使用 DLL 编写他们自己的应用程序,您应该将其安装在 GAC 中或让他们也复制 DLL。拥有库的多个副本听起来不是一件好事,但它确实是:这意味着您可以将一个副本升级到不同的版本而不会破坏其他所有内容。
回答by Richard
You can log, and then view the actions the framework took trying to load an assembly. This makes diagnosing assembly load errors very easy.
您可以记录,然后查看框架在尝试加载程序集时所采取的操作。这使得诊断程序集加载错误非常容易。
The tool to do both is "FUSLOGVW.exe" (Fusion Log Viewer, Fusion being the name of the loader) and included in the SDK.
执行这两项操作的工具是“FUSLOGVW.exe”(Fusion 日志查看器,Fusion 是加载程序的名称)并包含在 SDK 中。
回答by user76035
In your Main:
在您的主要:
AppDomain.CurrentDomain.AssemblyResolve += (s,e)=>{
var filename = new AssemblyName(e.Name).Name;
var path = string.format(@"C:\path\to\assembly\{0}.dll", filename);
return Assembly.LoadFrom(path);
};
Add some exception handling to that
添加一些异常处理
回答by Robert Baker
It's possible without the GAC, but assemblies must be strong named and you must make changes to app.config whenever the version or publickeytoken changes. This is what I have Main Program in \, Shared libs in \Shared. and a sub program I want separated in \SDK, it uses ..\Shared for the assemblies.
没有 GAC 也是可能的,但程序集必须是强命名的,并且您必须在版本或 publickeytoken 更改时对 app.config 进行更改。这是我在\中的主程序,在\Shared中的共享库。和我想在 \SDK 中分离的子程序,它使用 ..\Shared 作为程序集。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="shared" />
<dependentAssembly>
<assemblyIdentity name="protobuf-net" publicKeyToken="257b51d87d2e4d67" />
<codeBase version="1.0.0.282" href="../protobuf-net.dll"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="SevenUpdate.Base" publicKeyToken="5d1aea1de74f122c" />
<codeBase version="11.5.4.0" href="../SevenUpdate.Base.dll"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="SharpBits.Base" publicKeyToken="5d1aea1de74f122c" />
<codeBase version="11.5.5.0" href="../SharpBits.Base.dll"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Windows" publicKeyToken="5d1aea1de74f122c" />
<codeBase version="11.5.5.0" href="../System.Windows.dll"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WPFLocalizeExtension" publicKeyToken="5d1aea1de74f122c" />
<codeBase version="11.5.5.0" href="../WPFLocalizeExtension.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>