zoukankan      html  css  js  c++  java
  • How to: Determine If a File Is an Assembly(From msdn)

    A file is an assembly if and only if it is managed, and contains an assembly entry in its metadata. For more information on assemblies and metadata, see the topic Assembly Manifest.

    How to manually determine if a file is an assembly

    1. Start the MSIL Disassembler (Ildasm.exe).

    2. Load the file you wish to test.

    3. If ILDASM reports that the file is not a portable executable (PE) file, then it is not an assembly. For more information, see the topic How to: View Assembly Contents.

    How to programmatically determine if a file is an assembly

    1. Call the GetAssemblyName method, passing the full file path and name of the file you are testing.

    2. If a BadImageFormatException exception is thrown, the file is not an assembly.

    This example tests a DLL to see if it is an assembly.

    class TestAssembly
    {
        static void Main()
        {

            try
            {
                System.Reflection.AssemblyName testAssembly =
                    System.Reflection.AssemblyName.GetAssemblyName(@"C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll");

                System.Console.WriteLine("Yes, the file is an Assembly.");
            }

            catch (System.IO.FileNotFoundException)
            {
                System.Console.WriteLine("The file cannot be found.");
            }

            catch (System.BadImageFormatException)
            {
                System.Console.WriteLine("The file is not an Assembly.");
            }

            catch (System.IO.FileLoadException)
            {
                System.Console.WriteLine("The Assembly has already been loaded.");
            }
        }
    }
    /* Output (with .NET Framework 3.5 installed):
        Yes, the file is an Assembly.
    */

    The GetAssemblyName method loads the test file, and then releases it once the information is read.

  • 相关阅读:
    WebSocket
    牛人
    ECSHOP+wamp
    数据结构之-----------排序
    DRGS指标计算方法
    Oracle 11g安装教程
    JavaScript入门
    多态
    类的继承
    上传工具类
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1703663.html
Copyright © 2011-2022 走看看