【C#】Winform判断程序集是debug还是release版本

习惯了Unity和Android开发,需要发布到多个渠道(猪大家业务多多,赚大发)

在开发过程中,一般需要知道时Debug版本还是Releas版本

C# Code:

 

string  codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
返回结果:"file:///c:/users/administrator/documents/visual studio 2015/Projects/WindowsFormsApplication4/WindowsFormsApplication4/bin/Debug/WindowsFormsApplication4.EXE"  

string  file = Assembly.GetExecutingAssembly().GetName().Name;
返回结果:"WindowsFormsApplication4"  

string  location = Assembly.GetExecutingAssembly().Location;
返回结果:"c:\\users\\administrator\\documents\\visual studio 2015\\Projects\\WindowsFormsApplication4\\WindowsFormsApplication4\\bin\\Debug\\WindowsFormsApplication4.exe"  

string  fileName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
返回结果:"WindowsFormsApplication4.exe"

再可以用以下方法:

  或许在开发过程中,你会遇到这种情况,我们拿到一个dll或者exe,不知道这个程序集是debug还是release版本。其实C#开发中,我们是在JIT运行环境中来判断程序集,是否是debug还是release版本。直接上代码吧,如下是控制台程序,比较简单:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DebugBuild("D:\\Test.exe")); //这里就是要验证的程序集路径
        }
 
        private static bool DebugBuild(string path)
        {
            return DebugBuild(Assembly.LoadFile(Path.GetFullPath(path)));
        }
 
        private static bool DebugBuild(Assembly assembly)
        {
            foreach (object attribute in assembly.GetCustomAttributes(false))
            {
                if (attribute is DebuggableAttribute)
                {
                    DebuggableAttribute _attribute = attribute as DebuggableAttribute;
 
                    return _attribute.IsJITTrackingEnabled;
                }
            }
            return false;
        }
    }
}
       另外有关JIT知识补充点,有的时候,我们会遇到JIT运行时出错,解决方式参考上上篇文章:http://blog.csdn.net/u014650759/article/details/78042043
————————————————
版权声明:本文为CSDN博主「习明然」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014650759/article/details/78220796

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值