星期二, 4月 26, 2016

[C#] Reflection - Assembly GUID 資訊

實務上有需求要抓出 Assembly GUID 資訊,簡易紀錄
  • DemoEXE:開來抓 GUID 用,本身沒有寫任何 Code
  • DemoShowGUID:要在這裡抓 DemoEXE 和本身的 GUID 資訊
Reflection-Assembly GUID 資訊-4

using System.Runtime.InteropServices;
using System.Reflection;

namespace DemoShowGUID
{
    class Program
    {
        static void Main(string[] args)
        {
   
            // DemoEXE GUID:3088714c-9fa3-4d6a-8a00-2e743b93c09b
            string path = @"D:\Reflection_AssemblyInfo\DemoEXE\bin\Debug\DemoEXE.EXE";
            string DemoEXEInfo = ((GuidAttribute)Assembly.LoadFrom(path).GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value;

            // 抓本身 DemoShowGUID GUID:6fa73d73-6533-4d4c-9fcb-100a6b43b0cc
            string DemoShowInfo = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value;

            string message =
                "DemoEXE GUID 資訊:" + DemoEXEInfo + Environment.NewLine +
                "DenoShowInfo GUID 資訊:" + DemoShowInfo;
            Console.WriteLine(message);
        }
    }
}

DemoEXE Assembly 資訊

Reflection-Assembly GUID 資訊-1

DemoShowGUID Assembly 資訊

Reflection-Assembly GUID 資訊-2

結果
Reflection-Assembly GUID 資訊-3

沒有留言:

張貼留言