在C#.net中引用DLL [英] Referencing a DLL in C#.net

查看:134
本文介绍了在C#.net中引用DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要使用一些DLL的项目.我的项目Release文件夹中有一些DLL.但是对于DLL的位置之一是不固定的.即它可以在运行我的应用程序的另一台PC上更改.该DLL的位置在环境变量"TEMP"中可用.因此,现在我要引用%TEMP%/mydll.dll.如何在C#.net中引用此dll?

在此先感谢
Rohit

I am developing a project which requires to use some DLLs. I have some DLLs in my project Release folder. But for one of the DLL location is not fixed. i.e. it can change on different PC where I am running my application. Location of that DLL is available in Environment Variable say "TEMP". So now I want to refer to %TEMP%/mydll.dll . How can I refer to this dll in C#.net?

Thanks in Advance
Rohit

推荐答案

在开发过程中引用程序集并在运行时使用它是不同的事情.生成解决方案时,引用的程序集应在其中以允许生成.如果将其部署并放置在与引用程序集相同的目录中,它将可以正常工作.

如果您出于某种原因(顺便说一句,为什么?)而不想部署此程序集,但又希望要求它出现在目标系统中,该怎么办?这样的环境变量将无济于事,但原则上,您可以在安装期间使用它.

通常有两种方法可以解决引用的程序集.第一个是:您可以要求将其放置在GAC中.每个人都可以使用cagutil.exe来做到这一点.如何要求它是您的问题,但是如果将它正式提交给GAC(由于需要使用强命名程序集,则需要为此签名),它将始终能够得到正确解决.它还有助于唯一的版本控制(强命名,而不是GAC本身).

解决引用程序集的另一种方法是应用程序的标准配置文件.这是使用装配位置探测的示例:
Referencing the assembly during development and using it during run time are different things. When you build the solution, the referenced assembly should be there to allow the build. If you deploy it and place it in the same directory as the referencing assembly, it will work just fine.

What to do if you don''t want to deploy this assembly but you want to require that it should present in the target system, by some reason (by the way, why?!)? Environment variable as such won''t help you, but in principle, you can use it during installation.

There are generally two ways of resolving the referenced assembly. The first one is: you can require it to be placed in GAC. Everyone can do it using cagutil.exe. How to require it is your problem, but if it is formally submitted to GAC (you need to sign it just for this purpose as the strongly named assembly is required), it will always be resolved properly. It also helps unique versioning (strong naming, not GAC itself).

Another way of resolving the referenced assembly is the application''s standard configuration file. Here is the sample using assembly location probing:
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath=".\MyLibraries\MyAssemblies"/>
        </assemblyBinding>
    </runtime>
</configuration>



如果不使用此配置文件,则该应用程序将运行,并且引用的程序集应位于同一目录中.如果添加此文件并将其命名为"myApplication.exe.config"(其中"myApplication.exe"是应用程序条目程序集的主要可执行模块的名称),则运行时将尝试在以下位置查找所需的引用程序集在此示例中,相对于此可执行模块的位置,由privatePath指定的子目录.这样,您可以在位于不同目录中的应用程序之间重用一组引用.您可以开发一个部署过程,根据用户的可选参数或其他因素,编写适当的配置文件以及所需的目录位置进行探测.

-SA



If you don''t use this configuration file, the application will run, and the referenced assembly should be in the same directory. If you add this file and name it "myApplication.exe.config" (where "myApplication.exe" is the name of the main executable module of the application entry assembly), the run-time will try to find the required referenced assembly in the sub-directory specified by privatePath, in this example, relative to the location of this executable module. This way, you can reuse a set of referenced between applications located in different directories. You can develop a deployment procedures writing appropriate configuration files with required locations of directories for probing, depending on user''s optional parameters or other factors.

—SA


如果使用解决方案资源管理器中的添加引用"对话框创建对该dll的引用,该dll将自动复制到exe文件所在的文件夹中.因此,如果您的exe位于yourproject \ bin \ release中,则您将在同一文件夹中找到该项目引用的所有本地程序集的副本.
希望对您有帮助
If you create a reference to the dll using Add Reference dialogue in Solution Explorer, the dll is automatically copied to the same folder as the exe file. So if your exe is in yourproject\bin\release, you will find a copy of all local assemblies referenced by the project in the same folder.

Hope this helps


这篇关于在C#.net中引用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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