C#之判斷確定檔案是否為程式集

wisdomone1發表於2011-08-26
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace zxy_console
{
    class Program
    {
        static void Main()
        {
            try
            {
                 //反射 查詢應用程式集
                System.Reflection.AssemblyName testAssembly =
                    System.Reflection.AssemblyName.GetAssemblyName(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll");

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

            catch (System.IO.FileNotFoundException e)
            {
                System.Console.WriteLine("The file cannot be found.");
                System.Console.ReadKey();
            }
                //badimageformatexception表示不是應用程式集
            catch (System.BadImageFormatException e)
            {
                System.Console.WriteLine("The file is not an Assembly.");
                System.Console.ReadKey();
            }

            catch (System.IO.FileLoadException e)
            {
                System.Console.WriteLine("The Assembly has already been loaded.");
                System.Console.ReadKey();
            }
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-705906/,如需轉載,請註明出處,否則將追究法律責任。

相關文章