First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« SOLVED: How to Force IIS to load a certa... | Main | A New Private Browser - I mean Browzar -... »

Nagaraj from my company made this little util recently to run against a compiled assembly and see if it is a Debug or Release version. I added the DOS ErrorLevel return codes.

using System;

using System.IO;

using System.Diagnostics;

using System.Reflection;

 

namespace Foo.Tools

{

    class BuildFind

    {

        public static int GetBuildType(string AssemblyName)

        {

            Assembly assm = Assembly.LoadFrom(AssemblyName);

            object[] attributes = assm.GetCustomAttributes(typeof(DebuggableAttribute), false);

 

            if (attributes.Length == 0)

            {

                Console.WriteLine(String.Format("{0} is a RELEASE Build....", AssemblyName));

                return 0;

            }

            foreach (Attribute attr in attributes)

            {

                if (attr is DebuggableAttribute)

                {

                    DebuggableAttribute d = attr as DebuggableAttribute;

                    Console.WriteLine(
                       String.Format("Run time Optimizer is enabled : {0}", !d.IsJITOptimizerDisabled));

                    Console.WriteLine(
                        String.Format("Run time Tracking is enabled : {0}", d.IsJITTrackingEnabled));

                    if (d.IsJITOptimizerDisabled == true)

                    {

                        Console.WriteLine(String.Format("{0} is a DEBUG Build....", AssemblyName));

                        return 1;

                    }

                    else

                    {

                        Console.WriteLine(String.Format("{0} is a RELEASE Build....", AssemblyName));

                        return 0;

                    }

                }

            }

            return 3;

        }

 

        [STAThread]

        static int Main(string[] args)

        {

            if (args.Length == 0)

            {

                Console.WriteLine("Usage GetBuildType <assemblyName>");

                return 2;

            }

            return BuildFind.GetBuildType(args[0]);

        }

    }

}

Tracked by:
"Debug/Release Mode einer Assembly zur Laufzeit abfragen" (Good news everyone!) [Trackback]
"Programmatically Detect if an Assembly is Compiled in Debug or Release" (DotNet... [Trackback]
"Debug/Release Mode einer Assembly zur Laufzeit abfragen" (Good news everyone!) [Trackback]


Wednesday, August 30, 2006 3:41:46 PM (Pacific Standard Time, UTC-08:00)
Your Main method has a return type of void but you return 2?
Thursday, August 31, 2006 12:19:28 AM (Pacific Standard Time, UTC-08:00)
Nice. Have you seen Jeff Key's 'IsDebug' tool? (www.sliver.com) It does the same sort of thing, and comes with source. Gotta love Jeff Key. Very handy tool for discovering that most of your developers have been deploying debug assemblies to production... (slapped wrist)
AndyToo
Thursday, August 31, 2006 2:00:51 AM (Pacific Standard Time, UTC-08:00)
Thanks, Scott! I was looking for the solution for a long time.
Thursday, August 31, 2006 3:56:38 AM (Pacific Standard Time, UTC-08:00)
And for your own assemblies do everyone a favour and:...

AssemblyInfo.cs
----------------

#if DEBUG
[assembly: AssemblyTitle("My Cool App [Debug build]")]
#else
[assembly: AssemblyTitle("My Cool App [Retail]")]
#endif
Thursday, August 31, 2006 6:00:01 AM (Pacific Standard Time, UTC-08:00)
Why the Console.WriteLine(String.Format("...."))?

Console.WriteLine has string formating built in.. So you could just do:

Console.WriteLine("{0} is debug..", Assemblyname);

Anyhow, nifty little tool. Actually discussed this with my co-worker the other day, as we're moving into the final stage of the development cycle we want to automate the deployment into the production environment, and only want to deploy RELEASE assmelbies.
Thursday, August 31, 2006 6:39:44 AM (Pacific Standard Time, UTC-08:00)
Jonas, it's inherited code, but I'll remind the author!
Scott Hanselman
Friday, September 01, 2006 10:21:10 AM (Pacific Standard Time, UTC-08:00)
Scott,

I know it is inherited code, but I am wondering about the return codes.

Seems like you can get console output indicating DEBUG and return code 1.
Then seems like you can get console output indicating RELEASE and return codes 0 or 1.

So it seems like you would never *know* that it was a "DEBUG" build. Then again
I could easily have missed something :-)

John.
John
Friday, September 01, 2006 11:04:45 AM (Pacific Standard Time, UTC-08:00)
Sweet, I've wanted to know how do to this for ages...
Thanks Scott (and Nagaraj)!
-A
Friday, September 01, 2006 12:59:42 PM (Pacific Standard Time, UTC-08:00)
John - You're right...I got sloppy. I'll fix it.
Scott Hanselman
Friday, September 01, 2006 1:30:45 PM (Pacific Standard Time, UTC-08:00)
Scott

No worries.

I guess only bottom (command line) dwellers would care. Talking of which, it seems a perfect candidate for some PowerShell magic. Maybe even making the script follow dependencies. Dependencies would be easier as you could handle an array in PS. That way, you could see if your entire product had any debug components.

Just a thought :-)

John.
John
Friday, September 01, 2006 7:06:13 PM (Pacific Standard Time, UTC-08:00)
A few days ago I wrote a Powershell script to do the same thing. Hopy some people will find it usefull.

http://guessman.blogspot.com/2006/08/first-time-scripting-with-powershell.html

function IsDebug() {
process
{
trap {continue;}
$asmb = $null;
$asmb = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($_.FullName);
if ($asmb)
{
$Attribs = [System.Reflection.CustomAttributeData]::GetCustomAttributes($asmb);
foreach ($attrib in $Attribs)
{
if ($attrib.ToString().StartsWith("[System.Diagnostics.DebuggableAttribute") -AND
-NOT $attrib.ToString().StartsWith("[System.Diagnostics.DebuggableAttribute((Boolean)False") )
{
$_.Name + " Is DEBUG " + $attrib.ToString();
}
}
}
}
}

===

dir | IsDebug($_)
===
Timur
Saturday, September 02, 2006 10:45:25 AM (Pacific Standard Time, UTC-08:00)
For my part, I always make sure that the following snippet of code is in my AssemblyInfo.cs:

#if (Debug || DEBUG)
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
Comments are closed.

Contact

Sponsors

Hosting By

On this page...

Tags

Calendar

<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Archives

Google Ads