如何找到程序集名称 [英] how to find assembly name

查看:94
本文介绍了如何找到程序集名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用反射来查找程序集的内部内容.现在我想知道在特定路径中是​​否存在具有指定版本的指定dll.请在c#.net

Hi,
I am using reflection to find inner content of assembly. Now i want to know whether the specified dll with specified version is present in a perticular path or not. please give code in c#.net

推荐答案

中提供代码.我假设指定的DLL…特定路径"意味着您知道程序集主要可执行模块的确切完整路径名. (通常,这只是一个DLL文件,因为Visual Studio仅支持单模块程序集.).

因此,在检查文件是否存在后,问题就减少到了将版本与某些必需版本进行比较的情况. (如果您不知道确切的文件名,则只需对目录中的所有文件应用相同的方法.)

方法如下:
I assume "specified DLL… in particular path" means you know exact full path name of the assembly''s main executable module (usually, this is just one DLL file as Visual Studio supports only single-module assemblies).

So, the problem is reduced to the comparison of the version with some required version after you check that the file exists. (If you don''t know exact file name you should simply apply the same method to all files in the directory.)

Here is how:
enum VersionCheckResult {
    FileNotFound, NotAValidAssembly,
    VersionEarlierThenRequired, VersionLaterThenRequired,
    VersionMatch, }

VersionCheckResult CheckAssemblyFile(
    string fileName,
    System.Version requiredVersion) {
    if (!System.IO.File.Exists(fileName))
        return VersionCheckResult.FileNotFound;
    try {
        System.Reflection.Assembly assembly=
            System.Reflection.Assembly.LoadFrom(fileName);
        System.Version version = assembly.GetName().Version;
        if (version < requiredVersion)
            return VersionCheckResult.VersionEarlierThenRequired;
        else if (version > requiredVersion)
            return VersionCheckResult.VersionLaterThenRequired;
        else
            return VersionCheckResult.VersionMatch;
    } catch { //exclusion from the common rule of exceptions use
        return VersionCheckResult.NotAValidAssembly;
    } //exception
}





如果我没有正确阅读您的问题;您可能会说名称不是路径名,而是程序集的强名称.在这种情况下,您应该比较名称,而不是版本(或不仅仅是版本).在这种情况下,Assembly.GetNameAssembly.Location —它会为您提供程序集的主要可执行模块的确切完整路径;您可以使用System.IO.Path.GetDirectoryName提取目录名称.例如,如果程序集位于CAG中,则可以使用其名称而不是使用Assembly.Load(AssemblyName)的路径来加载程序集.成功完成后,您可以使用<code> Assembly.Location.
找到它的路径名.
这样,您可以进行往返:从路径名开始查找字符串程序集名称,反之亦然.
请参阅 http://msdn.microsoft.com/en-us/library/system. Reflection.assembly.aspx [ ^ ]以获得更多详细信息.

—SA





If I did not read your question correctly; you might mean by Name not the path name but assemblies strong name. In this case, you should compare the name, not version (or not just version). In this case Assembly.GetName and Assembly.Location — it will give you the exact full path of the assembly''s main executable module; you can extract the directory name using System.IO.Path.GetDirectoryName. For example, if your assembly is in CAG, you can load the assembly using its name rather then path using Assembly.Load(AssemblyName). When it is successfully done, you can find out it''s path name using <code>Assembly.Location.

In this way, you can do a round trip: starting from the path name find string assembly name and visa versa.
Please see http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^] for more detail.

—SA


看看
确定程序集的全限定名称[
Take a look
Determine an Assembly''s Fully Qualified Name[^]


看看这篇文章. http://vbcity.com/blogs/jatkinson/archive/2010/01/03/reading-the-global-assembly-cache-gac-in-vb-net.aspx [
Have a look at this article. http://vbcity.com/blogs/jatkinson/archive/2010/01/03/reading-the-global-assembly-cache-gac-in-vb-net.aspx[^]

It will give you the list of all Assemblies along with it''s version. Then you can compare these versions with the one that you extracted using Reflection.

Hope it helped. Good luck. :thumbsup:

UPDATE:
Here is the C# code:

string utilPath = @"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe";

            Process gacProcess = new Process();
            gacProcess.StartInfo.FileName = utilPath;
            gacProcess.StartInfo.CreateNoWindow = true;
            gacProcess.StartInfo.RedirectStandardOutput = true;
            gacProcess.StartInfo.UseShellExecute = false;
            gacProcess.StartInfo.Arguments = "/l";

            gacProcess.Start();

            StreamReader gacOutput = gacProcess.StandardOutput;

            while (!gacOutput.EndOfStream)
            {
                string output = gacOutput.ReadLine();
                try
                {
                    AssemblyName asm = new AssemblyName(output);
                    string version = asm.Version.ToString();  //Here you will get the version of the assembly
                }
                catch (Exception ex)  //Catch the exception but do nothing
                { }                
            }

            gacOutput.Close();


您也可以使用此工具:
http://www.developerfusion.com/tools/convert/vb- to-csharp/ [ ^ ]将vb.net转换为c#


Also you can use this tool: http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] to convert vb.net to c#


这篇关于如何找到程序集名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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