如何使用Csharp获取Refrence Dll路径? [英] How to get Refrence Dll Path Using Csharp?

查看:63
本文介绍了如何使用Csharp获取Refrence Dll路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类库项目,并添加了X.dll和y.dll,并且正在构建它.它创建Classlibrary.dll.该classlibrary.dll由X.dll和y.dll组成.

现在,我正在创建一个新的Winforms应用程序,并添加ClassLibrary.dll作为参考.现在,如果我运行Winforms应用程序,我想找到X.dll和Y.dll的引用路径.
我试过了,但它显示Winforms本地bin文件夹为路径

I created a class library project and added X.dll and y.dll and I am building it. It creates Classlibrary.dll. This classlibrary.dll consists of X.dll and y.dll.

Now I am creating a new Winforms application and adding ClassLibrary.dll as a reference. Now if I run the Winforms application I want to find the reference path of X.dll and Y.dll

I tried this but it shows Winforms local bin folder as the path

MyAssembly = Assembly.LoadFrom("Classlibrary.Dll");
  string asembl = GetCurrentExecutingDirectory(MyAssembly);


 public static string GetCurrentExecutingDirectory(Assembly assemb)

{

       string filePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath;

         return Path.GetDirectoryName(filePath);
     }


它仅显示本地路径文件夹,例如D:\ Winform \ bin \ debug \ Winform.dll或winform.exe ...这是不对的.我需要GAC的路径..pls谁都可以帮助我


It shows only local path folder like D:\Winform\bin\debug\Winform.dll or winform.exe ... this is not right . i need path from GAC ..pls any one help me

推荐答案

粗略地说,GAC的全部目的是您永远不需要它的路径.您只需按其全名引用程序集即可.若要查看其在项目中的完成方式,请参阅如何引用与.NET捆绑在一起的某些程序集,例如"System.Windows.Forms.dll".另请参阅我对问题的评论.

即使需要从GAC动态加载程序集,您也应该使用System.Reflection.Assembly.Load(string assemblyString),请参见此页面上的示例:
http://msdn.microsoft.com/en-us/library/ky3942xh.aspx [ ^ ].

您为当前执行目录"编写的代码存在疑问.您可以1)调用函数的程序集的位置; 2)更常用的是入口组件主模块的位置; 3)工作目录.
请注意,您的应用程序的不同程序集可以放置在不同的目录中.根据配置文件或代码,有不同的程序集解析方法.

正确的方法是:
Roughly speaking, the whole purpose of GAC is that you never need a path from it. You just reference an assembly by its full name. To see how it''s done in the project, please see how some assembly bundled with .NET is referenced, such as "System.Windows.Forms.dll". Please also see my comments to the question.

Even if you need to load the assembly from the GAC dynamically, you should rather use System.Reflection.Assembly.Load(string assemblyString), please see the example on this page:
http://msdn.microsoft.com/en-us/library/ky3942xh.aspx[^].

You code for "current executing directory" is questionable. You can have 1) location of the assembly where you function is called; 2) more usually used, the location of the main module of the entry assembly; 3) working directory.
Note that different assemblies of your application can be placed in different directories; there a different methods of assembly resolution, based on config file or code.

Right methods would be:
static string GetAssemblyDirectory(System.Reflection.Assembly assembly) {
    return System.IO.Path.GetDirectoryName(assembly.Location);
}

//...

string callerAssemblyLocation = GetAssemblyDirectory(System.Reflection.Assembly.GetExecutingAssembly());
string entryAssemblyLocation = GetAssemblyDirectory(System.Reflection.Assembly.GetEntryAssembly());
string workingDirectory = System.IO.Directory.GetCurrentDirectory();



请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ].

—SA



Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].

—SA


这篇关于如何使用Csharp获取Refrence Dll路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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