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!

IntegerWolf
  • 1,232
  • 1
  • 11
  • 21
  • 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. – AnorZaken Jul 07 '22 at 11:51

3 Answers3

128

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).
Stefan
  • 919
  • 2
  • 13
  • 24
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • 5
    I was needed to restart visual studio to make my configuration symbol working – Jerome2606 Sep 05 '18 at 12:36
  • Which file does the "Active solution configuration" setting change? I'm wanting to commit this in git but it isn't seeing the change – Goku Nov 07 '19 at 16:44
  • @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". – Dennis Nov 13 '19 at 08:26
  • VS 2022 17.5.1 - Create new config in step 1 and 2. Then it is simply available (as uppercase!) – AC Thompson Mar 14 '23 at 19:02
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.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
  • right, happy to find both responses. This makes my life much easier. Thanks ! – user1841243 May 27 '15 at 14:00
  • 2
    In addition to unchecking `Define DEBUG constant` I had to add a semi-colon after `OFFLINE` in the `Conditional complication symbols` text box – gblock Jan 05 '19 at 01:02
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.

user3293338
  • 481
  • 1
  • 6
  • 16
  • 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. – Dennis Nov 13 '19 at 08:31