views:

773

answers:

3

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'

+8  A: 

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;
#end if 
}
JaredPar
+1: beaten by seconds
Fredrik Mörk
Under the default configurations provided by Visual Studio when you first create a project this is true, but it is possible to define the DEBUG macro in other build types as well if you choose. This will however only determine the build of the project and not whether it is running with the debugger as per the original question.
Martin Robins
@Martin, the original question is to look for project configuration. It was later updated to say running with the debugger attached. Yes, people can be evil and made Debug not define DEBUG which is why I said explicitly labeled this as a "guess" vs. a definitive solution.
JaredPar
For C#, "#end if" should be "#endif"
Babak Naffas
+1  A: 

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

Martin Robins
A: 

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.

Joel Goodwin