0

I am attempting to make it so that we can have our application to behave differently based on the presence of a preprocessor directive. I've read that you can create build configurations to define different preprocessor directives based on which build you do. Well, I am not seeing anything in Visual Studio to do this.. I know how to do it from the command line, but not how to do it within the automated environment of VS 2008.

Can someone tell me how to create a new build configuration which has preprocessor directives set in it?

Also, not sure if it has anything to do with it, but our project is an ASP.Net website

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • IMO, this sounds like it would be better done via either web.config, or something like IoC/DI. – Marc Gravell Dec 29 '09 at 22:59
  • our web.config is in source control. We want to be able to test each configuration without changing files in source control. – Earlz Dec 29 '09 at 23:01
  • 2
    You may have trouble finding information about this, since there is no preprocessor in C#. You're looking for "conditional compilation". – John Saunders Dec 29 '09 at 23:01
  • yes, I know this.. I'm not seeing anyway to do a conditional compilation with asp.net though – Earlz Dec 29 '09 at 23:07
  • Web Site Project. What is the difference? – Earlz Dec 29 '09 at 23:13
  • A web site project is more of a "convention over configuration" type of project, which doesn't use a .csproj file. The web application project uses a csproj file, and does not depend on folder structure, etc. for defining the project. Web Site projects have gotten a reputation for being difficult to use in non-trivial projects, especially when source control is involved. Maybe look into converting or re-creating your site as a Web Application, rather than a Web Site. Here's some more info: http://msdn.microsoft.com/en-us/library/aa730880%28VS.80%29.aspx#wapp_topic5 – Andy White Dec 29 '09 at 23:19
  • @Andy and/or John, why don't you make an answer so I can mark it as correct! That is the problem.. using a web site project rather than an application – Earlz Dec 29 '09 at 23:22

2 Answers2

6

If you right-click the project and select "Properties", then select the Build tab, you can enter custom compilation symbols in "Conditional compilation symbols".

For example, if your code looks like this:

#if DEBUG
// do something
#else
// do something else
#end

You can set "DEBUG" as a Conditional compilation symbol.

You can set different values for different build configurations, by changing the "Configuration" drop-down.

Andy White
  • 86,444
  • 48
  • 176
  • 211
  • this is what I read everywhere and the only thing I see is "start action" "target framework" "build solution action" "accessibility validation".. nothing about the preprocessor is listed anywhere that I can find. @your edit: there does not exist a tab or anything for entering "conditional compilation symbols" – Earlz Dec 29 '09 at 22:59
  • Interesting, what type of project is it? On my build tab, it shows sections like "General", "Errors and Warnings", "Treat warnings as errors" and "Output". The "Conditional compilation symbols" section is under General. – Andy White Dec 29 '09 at 23:04
  • I do not even see a way to control the DEBUG symbol. and it is an ASP.Net website. I'll post a screen shot. – Earlz Dec 29 '09 at 23:08
6

I do not even see a way to control the DEBUG symbol, and it is an ASP.Net website

Ok, there's the problem. The ASP.net web site option gives you a lot less control over builds, etc as it doesn't even use a project file.

If it is at all feasible for you to switch, switch over to an ASP.net web application. A web app will behave much more like a typical C# project (you have a project file, and the contents of the csproj file control what is in the app instead of just the directory structure, etc.

After you have converted, you should see the options that you are expecting.

Here's a link with directions for converting: How To Convert ASP.NET Website to ASP.NET Web Application

Community
  • 1
  • 1
JMarsch
  • 21,484
  • 15
  • 77
  • 125