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测试了一下,这种找特征的方法并不是每次有效,因此我主要写了个小工具实现了第二个方法。

建立C#窗体应用程序(Winform)如下:

程序代码如下:

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;
            }
        }
    }
}

程序运行效果如下:

1、检测到dll文件是在Debug模式下编译的

2、 检测到dll文件是在Release模式下编译的

3、不是C#(或VB.NET等) 语言生成的dll,工具会直接提示异常信息

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

END

转载于:https://my.oschina.net/Tsybius2014/blog/682887

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值