49

I have an application installed on my computer. How do I find out if it was compiled in DEBUG mode or not?

I've tried to use .NET Reflector, but it does not show anything specific. Here is what I see:

// Assembly APPLICATION_NAME, Version 8.0.0.15072
Location: C:\APPLICATION_FOLDER\APPLICATION_NAME.exe
Name: APPLICATION_NAME, Version=8.0.0.15072, Culture=neutral, PublicKeyToken=null
Type: Windows Application
Marc.2377
  • 7,807
  • 7
  • 51
  • 95
  • similars questions in Stackoverflow, one question, and many, many different answers: http://stackoverflow.com/questions/654450/programatically-detecting-release-debug-mode-net http://stackoverflow.com/questions/798971/how-to-idenfiy-if-the-dll-is-debug-or-release-build-in-net http://stackoverflow.com/questions/194616/how-to-tell-if-net-app-was-compiled-in-debug-or-release-mode http://stackoverflow.com/questions/50900/best-way-to-detect-a-release-build-from-a-debug-build-net http://stackoverflow.com/questions/890459/asp-net-release-build-vs-debug-build – Kiquenet Feb 03 '11 at 19:56

5 Answers5

30

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
  • 29,603
  • 12
  • 67
  • 114
  • As I said, it's a couple of years ago when I did the investigation work... Who knows what's changed in the framework, or even how poor a job I did originally. ;) – ZombieSheep Oct 11 '08 at 22:29
  • Wonderful code! I created a console application integrated in my build machine to validate that final application to deliver will not be in debug mode. – Samuel May 25 '12 at 18:29
  • 3
    Please see my answer below on why this method of checking for DEBUG build is not the best solution. – Dave Black Aug 15 '13 at 22:29
  • FYI - I just used Visual Studio 2013 built debug and release builds of same dll and ran the methods above (using LINQPad) and both times the script believed the DLL in question was a release build. – raddevus Sep 19 '17 at 20:46
26

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.
Dave Black
  • 7,305
  • 2
  • 52
  • 41
  • Thanks for providing a detailed explanation of the different aspects and types of "debug builds". I'll be sure to point people toward your blog the next time I see some iffy implementations. – ulty4life Oct 25 '13 at 16:32
8

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(...)]
Joe Basirico
  • 1,935
  • 16
  • 17
  • 8
    Not true; i just checked an assembly that i built in Release mode. It still had this attribute: [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] – Robert Taylor Oct 11 '08 at 21:17
  • 3
    I compiled in release mode and i saw this in reflector: [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] – chikak Dec 04 '09 at 13:14
  • 5
    When I disassemble an assembly in Debug Mode, I see this: [assembly: Debuggable(DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.Default)] When I build it in Release Mode, the debuggable attribute only has the IgnoreSymbolStoreSequencePoints flag, as you all mention. – Alric Aug 06 '10 at 20:17
  • Your answer is incorrect - the DebuggableAttribute is present if you compile in Release mode and choose DebugOutput to anything other than "none". – Dave Black Nov 02 '15 at 18:34
2

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:

Max
  • 7,408
  • 2
  • 26
  • 32
  • 1
    This answer is incorrect. See my detailed explanation why this is incorrect here: https://stackoverflow.com/questions/798971/how-to-identify-if-the-dll-is-debug-or-release-build-in-net/5316565#5316565 – Dave Black Feb 25 '19 at 02:55
2

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.

flipdoubt
  • 13,897
  • 15
  • 64
  • 96
  • 2
    +1, but there's no need to decompile the utility - the author provides the source code: http://www.sliver.com/Downloads/IsDebugSource.zip – Luke Girvin Dec 21 '09 at 11:14
  • 1
    Link in answer is outdated - *"This website is for sale!"* – Pang Jul 08 '20 at 00:18