包含引用的程序集,而不必将应用程序保留在与引用的程序集相同的目录中
我正在将 HTMLAgilityPack
与我的 C# winform
应用程序一起使用。我使用 Visual Studio
从硬盘驱动器加载 HTMLAgilityPack 作为引用程序集。然后,我构建应用程序并将输出可执行文件复制到新目录。如果 HTMLAgilityPack 与我的输出可执行文件不在同一目录中,则在尝试运行我的可执行文件时会出现错误,提示“找不到引用”。是否有某种方法可以将引用嵌入到可执行文件中,以便用户在希望移动文件时无需随身携带 HTMLAgilityPack?
我期待您的回复,
埃文
I am using HTMLAgilityPack
with my C# winform
application. I loaded HTMLAgilityPack from my hard drive using Visual Studio
, as a Referenced Assembly. I then build my application and copy the output executable file to a new directory. If HTMLAgilityPack is not in the same directory as my output executable, an error occurs when attempting to run my executable saying that the 'reference could not be found.' Is there some way to have the reference embedded into the executable file so that users will not need to carry around HTMLAgilityPack with them whenever they wish to move the file?
I look forward to your responses,
Evan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以做到这一点 - 但这并不完全是微不足道的。您可以订阅
AppDomain.CurrentDomain.AssemblyResolve
事件,并在引发时检查它是否正在寻找 html 敏捷包(通过事件参数),然后获取程序集来自嵌入资源的 BLOB,使用Assembly.Load(theBlob)
,并返回Assembly
实例。另一种更简单的方法可能是
ilmerge.exe
最后,GAC 将不再需要在本地保存该文件……而是要求将其正式集中安装。说实话,我会避开这里的 GAC。
Yes, you can do that - but it isn't exactly trivial. You would subscribe to the
AppDomain.CurrentDomain.AssemblyResolve
event, and when raised, check that it is looking for html agility pack (via the event args), then fetch the assembly BLOB from the embedded resource, useAssembly.Load(theBlob)
, and return theAssembly
instance.Another, simpler, approach might be
ilmerge.exe
Finally, the GAC would avoid the need to have the file locally... but requires having it formally installed centrally instead. To be honest I would avoid the GAC here.
您可以使用 ILMerge 实用程序将多个程序集合并到单个文件中
ILMerge.exe /t:winexe /out:test.exe test1.exe test2.dll
You can use ILMerge utility to compine several assemblies into single file
ILMerge.exe /t:winexe /out:test.exe test1.exe test2.dll