Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

I blogged this a long time ago, and I don't know if it still valid or not, but the code is something like...

private void testfile(string file)
{
    if(isAssemblyDebugBuild(file))
    {
        MessageBox.Show(String.Format("{0} seems to be a debug build",file));
    }
    else
    {
        MessageBox.Show(String.Format("{0} seems to be a release build",file));
    }
}    

private bool isAssemblyDebugBuild(string filename)
{
    return isAssemblyDebugBuild(System.Reflection.Assembly.LoadFile(filename));    
}    

private bool isAssemblyDebugBuild(System.Reflection.Assembly assemb)
{
    bool retVal = false;
    foreach(object att in assemb.GetCustomAttributes(false))
    {
        if(att.GetType() == System.Type.GetType("System.Diagnostics.DebuggableAttribute"))
        {
            retVal = ((System.Diagnostics.DebuggableAttribute)att).IsJITTrackingEnabled;
        }
    }
    return retVal;
}

ZombieSheep's answer is incorrect.

My answer to this duplicate question is here:How to tell if a .NET application was compiled in DEBUG or RELEASE mode?

Be very careful - just looking at the 'assembly attributes' in the Assembly Manifest for the presence of the 'Debuggable' attribute does NOT mean that you have an assembly that is not JIT optimized. The assembly could be JIT optimized but have the Assembly Output under Advanced Build settings set to include 'full' or 'pdb-only' info - in which case the 'Debuggable' attribute will be present.

Please refer to my posts below for more info: How to Tell if an Assembly is Debug or Release and How to identify if the DLL is Debug or Release build (in .NET)

Jeff Key's application doesn't work correctly, because it identifies a "Debug" build based on if the DebuggableAttribute is present. The DebuggableAttribute is present if you compile in Release mode and choose DebugOutput to anything other than "none".

You also need to define exaclty what is meant by "Debug" vs. "Release"...

  • Do you mean that the application is configured with code optimization?
  • Do you mean that you can attach the Visual Studio/JIT Debugger to it?
  • Do you mean that it generates DebugOutput?
  • Do you mean that it defines the DEBUG constant? Remember that you can conditionally compile methods with the System.Diagnostics.Conditional() attribute.

You're on the right path actually. If you look in the Disassembler window in reflector you will see the following line if it was built in debug mode:

[assembly: Debuggable(...)]

How about using Jeff Key's IsDebug utility? It is a little dated, but since you have Reflector you can decompile it and recompile it in any version of the framework. I did.


Here is the VB.Net version of the solution proposed by ZombieSheep

Public Shared Function IsDebug(Assem As [Assembly]) As Boolean
    For Each attrib In Assem.GetCustomAttributes(False)
        If TypeOf attrib Is System.Diagnostics.DebuggableAttribute Then
            Return DirectCast(attrib, System.Diagnostics.DebuggableAttribute).IsJITTrackingEnabled
        End If
    Next

    Return False
End Function

Public Shared Function IsThisAssemblyDebug() As Boolean
    Return IsDebug([Assembly].GetCallingAssembly)
End Function

Update
This solution works for me but, as Dave Black pointed out, there may be situation where a different approach is needed.
So maybe you can also take a look a Dave Black's answer:

  • How to identify if the DLL is Debug or Release build (in .NET)
  • How to tell if a .NET application was compiled in DEBUG or RELEASE mode?