Douglas Anderson
Douglas Anderson

Reputation: 4690

Determine Solutions Configuration (Visual Studio)

Is there a way, in code, to determine what "Solutions Configuration" you are running in? For example, 'Debug' vs. 'Release?

I have a service that I like to test in the IDE in Debug, right now I have bool that I set which either runs the 'service' if set to true (which then uses the OnStart method to run my 'main' method), if it's set to false I just run the 'main' method. This works great but I often forget to reset the bool after testing and then when I go to install the service it fails and I have to go back, reset the bool, recompile etc.

If I could just determine programatically that I was running in the IDE in Debug then I could get around this issue.

Edit: While thinking this through, I guess what I really need in the end is to determine if I'm in the 'playing' the app in the ide and not the soulutions configuration. This would allow me to compile in either debug or other configuration.

The simpliest solution seems to check 'System.Diagnostics.Debugger.IsAttached'

Upvotes: 1

Views: 4696

Answers (3)

JaredPar
JaredPar

Reputation: 754585

You can't directly look at the solution configuration, but you can use a few clues to "guess" what version you are in. For instance, the DEBUG preprocessor macro will only be defined in the Debug solution configuration for C#.

bool InDebugConfiguration() {
#if DEBUG
  return true;
#else
  return false;
#endif 
}

Upvotes: 14

Joel Goodwin
Joel Goodwin

Reputation: 5086

Off the top of my head, you could also add a post-build event to copy in a config file:

if $(ConfigurationName)==Debug goto DEBUG_POSTBUILD
goto RELEASE_POSTBUILD


REM -----------DEBUG-----------    
:DEBUG_POSTBUILD
echo POSTBUILD-Debug Config Copy
copy "$(ProjectDir)\config_debug.cfg" "$(TargetDir)\config.cfg" /y
if errorlevel 1 goto FAILED

...then access the config file during the run. This isn't tied to the code build itself though.

Upvotes: 0

Martin Robins
Martin Robins

Reputation: 6163

To determine whether you are running under debug in the IDE, look at the Debugger class, specifically the IsAttached property...

Upvotes: 2

Related Questions