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
    评论
C#中,可以使用以下两种方法将DLL文件打包一个EXE程序中: 1. 使用ILMerge工具 ILMerge是一个免费的工具,可以将多个.NET程序集合并成一个程序集。你可以将所有的DLL文件和EXE文件都放到同一个文件夹中,然后使用ILMerge工具将它们合并成一个EXE文件。 使用ILMerge的命令行如下: ``` ILMerge.exe /out:MyApp.exe MyApp.exe MyDll1.dll MyDll2.dll ``` 其中,/out参数指定合并后的程序集名称,MyApp.exe是主程序集,MyDll1.dll和MyDll2.dll是要合并的程序集。 2. 使用Visual Studio的资源文件 另一种方法是将DLL文件作为资源文件嵌入到EXE程序中。在Visual Studio中,你可以将DLL文件添加到项目中,然后设置它们的Build Action为Embedded Resource。 然后,在代码中,你可以使用Assembly类来访问嵌入的资源。例如: ```csharp using System.Reflection; // 加载嵌入的DLL文件 Assembly assembly = Assembly.LoadFrom("MyApp.exe"); // 获取嵌入的资源流 Stream stream = assembly.GetManifestResourceStream("MyDll1.dll"); // 读取资源流中的数据 byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); // 将数据加载到内存中 Assembly dll = Assembly.Load(buffer); ``` 这个例子中,MyApp.exe是主程序集,MyDll1.dll是嵌入的DLL文件。使用Assembly.LoadFrom方法加载主程序集,然后使用Assembly.GetManifestResourceStream方法获取嵌入的资源流。最后,使用Assembly.Load方法将数据加载到内存中,这样就可以使用嵌入的DLL文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值