DLL必须与可执行文件存在于同一文件夹中 [英] DLL must exist in same folder as executible

查看:247
本文介绍了DLL必须与可执行文件存在于同一文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在MS Visual Studio 2010中使用C#创建了我的第一个DLL.我的可执行文件也是在C#中创建的,一切正常.我唯一遇到的问题是,仅当DLL位于同一文件夹中时,我的程序才运行.请告诉我我需要在程序或dll中进行哪些更改,以便能够将DLL放在C:\ Windows \ system32文件夹中,并在其他位置运行我的程序.

谢谢您.

I just created my first DLL using C# in MS Visual Studio 2010. My executible was also created in C# and everything works fine. The only issue I am having is, my program only runs if the DLL is in the same folder. Please tell me what I need to change in either my program or dll so I will be able to place the DLL in the C:\Windows\system32 folder and run my program for a differnt location.

Thank you

推荐答案

是的,您可能需要在其他文件夹中安装某些库程序集.例如,如果您有一些使用共享库的不同应用程序,并且不想将它们混合在一起或将任何内容放入 GAC ,则需要使用它.这是基于应用程序配置文件的方法之一:

假设输入程序集的主要可执行模块是"myApplication.exe",那么您可以使用以下内容创建文件"myApplication.exe.config":
Yes, you may need some library assemblies in a different folder. For example, you need it if you have some different applications using shared libraries and you don''t want to mix them together or put anything in GAC. Here is one of the methods based on application config file:

Let''s assume the main executable module of your entry assembly is "myApplication.exe", then you can create the file "myApplication.exe.config" with something like:
<configuration>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<probing privatePath="..\MyLibraries\MySharedAssemblies"/> <!-- or whatever directory you need -->
		</assemblyBinding>
	</runtime>
</configuration>


如果这样做,则可以将共享程序集放在目录".. \ MyLibraries \ MySharedAssemblies"(可以是其他任何目录)中.在此示例中,该路径是相对于您的入口程序集和配置文件的主要可执行模块的位置的,但是它可以是任何内容,包括绝对路径,但相对更易于管理.

还有其他解决装配位置问题的方法.我喜欢这个.

—SA


If you do this, you can place your shared assemblies in the directory "..\MyLibraries\MySharedAssemblies" (can be anything else). In this example, the path is relative to the location of the main executable module of your entry assembly and config file, but it could be anything, including absolute path, but relative is more manageable.

There are other methods of the resolution of assembly location; I prefer this one.

—SA


您还可以使用此事件来确定从何处加载您的dll:

You could also use this event to figure out where to load your dll from:

AppDomain.CurrentDomain.AssemblyResolve



我会不时地使用第三方dll,并将它们嵌入可执行文件中,以创建一个可以在任何地方使用的独立可执行文件.使用此事件,我可以访问嵌入式DLL,然后在运行时使用它们.



From time to time, I''d like to work with third party dlls and I embed them in the executable to create a standalone executable that could just work anywhere. Using this event allows me to reach embedded DLL''s and then use them at runtime.

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
			{
				string asmname = (new AssemblyName(e.Name)).Name;
				string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
				string resname = (from p in names
								  where p.Contains(asmname)
								  select p).FirstOrDefault();
				byte[] buffer = null;
				using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resname))
				{
					buffer = new byte[stream.Length];
					stream.Read(buffer, 0, buffer.Length);
				}
				return Assembly.Load(buffer);
			};


使用dotNet,您需要将程序集放在两个位置之一中,即全局程序集缓存(GAC)或与您的应用程序.
将装配放置在GAC中的唯一原因是,如果装配将被多个不同的应用程序使用.您还必须对程序集进行强命名,以便将其放入GAC.

为了更好地了解GAC,请阅读 [ ^ ]文档
With dotNet, you''ll want to have the assembly in one of two places, either the Global Assembly Cache (GAC), or in the same directory with your application.
The only reason for an assembly to be placed in the GAC is if your assembly is going to be used by multiple different applications. You will have to have your assembly strongly named as well in order to put it in the GAC.

To get a better understanding of the GAC, read this[^] documentation on it.


这篇关于DLL必须与可执行文件存在于同一文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆