#if debug --> #if myOwnConfig?
Asked Answered
T

3

85

is there a way in Visual Studio to use a custom Configuration like "#if DEBUG"

I need a "#if OFFLINE" as my Build-Config's name is "Offline" (only for further debug-purposes too)...

Thanks!

Tankersley answered 26/8, 2013 at 9:50 Comment(1)
Seems like VS does this for you by default nowadays. Not sure which version it changed in. But when I create a new Configuration (in 2022) there automatically exists a Compilation Symbol with the same name as the Configuration, which is nice.Tactics
A
129

Yes, this is possible.

Instructions:

  1. Build -> Configuration manager -> Active solution configuration -> New...
  2. Create a new configuration named: Offline.
  3. Project -> Properties -> Build -> Configuration -> Offline.
  4. Conditional compilation symbols, type: OFFLINE.
  5. Save project.
  6. Restart Visual Studio (reloading the project is not enough).
Annitaanniversary answered 26/8, 2013 at 9:57 Comment(4)
I was needed to restart visual studio to make my configuration symbol workingTrotline
Which file does the "Active solution configuration" setting change? I'm wanting to commit this in git but it isn't seeing the changeHollenbeck
@Goku: VS stores active config in local solution settings. It doesn't change anything inside project or solution files, thus it can't be stored in source control. This setting is a direction for VS which config to build by default (e.g. when you press F6). It behaves like "/p:Configuration" param for MSBuild, saying "Please, build my sources using %ConfigName% configuration".Annitaanniversary
VS 2022 17.5.1 - Create new config in step 1 and 2. Then it is simply available (as uppercase!)Ruffner
J
41

Yes, you can. But before you can, follow these steps:

  1. In Visual Studio, go to Properties -> Build.
  2. At the configuration dropdown, select "Offline"
  3. Add "OFFLINE" to the "Conditional compilation symbols" text box

If you skip these steps, your #if OFFLINE won't work because in that case, OFFLINE isn't defined.

Jaye answered 26/8, 2013 at 9:55 Comment(2)
right, happy to find both responses. This makes my life much easier. Thanks !Glasper
In addition to unchecking Define DEBUG constant I had to add a semi-colon after OFFLINE in the Conditional complication symbols text boxRadiosensitive
S
9

I followed exactly the same steps as above. But they are not working. They are missing one steps or they didn't explain this clearly.

When you are doing step 4. Conditional compilation symbols: type OFFLINE. You also need to uncheck "Define DEBUG constant". Otherwise the new symbol OFFLINE won't work.

I posted this here in case someone wasted lots of time as me when try to do the same thing.

Sthenic answered 20/2, 2018 at 17:23 Comment(1)
It looks like this depends on VS version and project system version. At least, when using VS2019/17 and netcore/netstandard projects, there's no need to restart VS and/or uncheck DEBUG/TRACE symbols. Honestly, I don't remember, how this behaved with earlier versions.Annitaanniversary

© 2022 - 2024 — McMap. All rights reserved.