C#:識別一個dll文件是Debug模式仍是Release模式編譯的

今天遇到一個問題,就是在要肯定一個C#項目中正在使用的一個dll文件是什麼模式編譯的。由於Debug和Release兩種模式編譯出的DLL,混用會有必定的風險。我在網上查了一些資料,最終找到了這篇文章:工具

http://www.codeproject.com/Articles/72504/NET-Determine-Whether-an-Assembly-was-compiled-in測試

這篇文章裏給出了兩個方法檢查dll文件是哪一個模式下編譯的,第一個方法是用.NET提供的反編譯工具ildasm。但我編譯了一組dll測試了一下,這種找特徵的方法並非每次有效,所以我主要寫了個小工具實現了第二個方法。ui

創建C#窗體應用程序(Winform)以下:spa

程序代碼以下:code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DebugReleaseTestor
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 測試指定文件是否爲Debug模式編譯
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        private bool IsAssemblyDebugBuild(string filepath)
        {
            Assembly assembly = Assembly.LoadFile(Path.GetFullPath(filepath));
            return assembly.GetCustomAttributes(false).Any(x =>
                (x as DebuggableAttribute) != null ?
                (x as DebuggableAttribute).IsJITTrackingEnabled : false);
        }

        /// <summary>
        /// 按鈕:查找文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.DefaultExt = "dll";
            ofd.Filter = "dll文件|*.dll|全部文件|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFilePath.Text = ofd.FileName;
            }
        }

        /// <summary>
        /// 按鈕:測試指定文件是否爲Debug模式編譯
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScan_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                string filePath = txtFilePath.Text;
                if (File.Exists(filePath))
                {
                    if (IsAssemblyDebugBuild(filePath))
                    {
                        result = "這是一個[DEBUG]模式下編譯的DLL文件";
                    }
                    else
                    {
                        result = "這是一個[RELEASE]模式下編譯的DLL文件";
                    }
                }
                else
                {
                    result = "文件不存在";
                }
                txtResult.Text = result;
            }
            catch (Exception ex)
            {
                txtResult.Text = ex.Message;
            }
        }
    }
}

程序運行效果以下:orm

一、檢測到dll文件是在Debug模式下編譯的get

二、 檢測到dll文件是在Release模式下編譯的string

三、不是C#(或VB.NET等) 語言生成的dll,工具會直接提示異常信息it

小工具的下載地址:http://pan.baidu.com/s/1gfjT2ltio

END

相關文章
相關標籤/搜索