I have DEBUG, RELEASE and MYMODE configuration modes. How can I detect them?
#if DEBUG
#endif
detect only DEBUG and anything else, but not difference between RELEASE and MYMODE.
How can I do it?
MORE INFO:
I was thinking about something like this:
#if DEBUG
#elif RELEASE
#elif MYMODE
but if I have RELEASE or MYMODE, all parts of code are gray, only for DEBUG mode the DEBUG part is colorful. I also tried
#define MYMODE
but for this, MYMODE is true all the time doesn't metter what mode is set.
First you can use boolean operators in #if:
#if DEBUG || MYMODE
…
#endif
Second you can use #else and #elif to break up things...
#if DEBUG
// Debug only
#elif MYMODE || RELEASE
// In either release or MYMODE
#else
// Otherwise
#endif
The combination of these two covers most use cases.
MyMode is a configuration. But, in and of itself, that doesn't define any conditional compilation symbols.
You change these through the projects compilation settings1 or by passing the -define option to csc. If you look through the Debug configuration's compilation settings, you'll find that the DEBUG conditional compilation symbol was already defined2, but there's no RELEASE symbol defined in the Release configuration.
There is no requirement (as you'll find above) that there be any relation between configurations and the symbols that they define.
#if (and family) is defined to work with conditional compilation.
1Project -> Properties -> Build -> General.
2In some versions of Visual Studio, there's a dedicated checkbox for it rather than it being listed in the Conditional Compilation symbols, but the effect is the same. If you unload the project and examine the XML, you'll find that all constants are stored in the <DefineConstants> element.
The same:
#if MYMODE
#endif
#if RELEASE
#endif
Or more complex:
#if (DEBUG && MYMODE)
#endif
#if (!RELEASE && MYMODE)
#endif
I found the solution also thanks by you guys. I used this code:
#if DEBUG
#elif MYMODE
#else
but first I needed to put the name MYMODE to the Properties > Build > Conditional Compilation Symbols. I didn't do it before so there was the problem.
Related
Is it possible to differentiate between operating systems in C# using preprocessor? like :
#if OS_WINDOWS
//windows methods
#elif OS_MAC
//mac methods
#elif OS_LINUX
//linux methods
#endif
What you are asking for is possible but needs a bit of work.
Define a preprocessor variable in your csproj
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<DefineConstants>_WINDOWS</DefineConstants>
</PropertyGroup>
Use that in your code
#if _WINDOWS
// your windows stuff
#else
// your *nix stuff
#endif
I find this technique useful when you have constants that are dependent on the OS (for example native library names)
No. Sadly you can't. And it is even logical: if you compile for AnyCPU, then your program is executable on any platform.
What you can do is create multiple project configurations, where you set the #define you want (in the Properties of the project, Build, Conditional compilation symbols).
But perhaps this is a XY problem... Normally you don't need to do it, and you can live with a
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
}
else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
{
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
}
Since MSBuild 15, there is a property function: IsOSPlatform().
It can be used to define OS-specific constants in the project file (*.csproj):
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<DefineConstants>OS_WINDOWS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<DefineConstants>OS_LINUX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('FreeBSD'))">
<DefineConstants>OS_FREEBSD</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<DefineConstants>OS_MAC</DefineConstants>
</PropertyGroup>
These constants can be checked in preprocessor directives, for example:
#if OS_WINDOWS
// Windows-specific code
#elif OS_LINUX
// Linux-specific code
#elif OS_FREEBSD
// FreeBSD-specific code
#elif OS_MAC
// Mac-specific code
#endif
No - think about it, the compiler runs once, but the same binary output can be used on multiple machines.
Now you can specify any symbols you want when you compile - so you could easily compile three different times and pass in different preprocessor symbols each time.
If you don't need any compile-time changes, you can just use Environment.OSVersion to detect the operating system you're running under.
I have simple code with compiler directive in my WPF app:
#if (DEBUG)
MySettings.Default.Host = "http://localhost:63372/";
#else
MySettings.Default.Host = "http://example.com/";
#endif
All works fine in the Visual Studio. When I switch to Release or Debug then Host filled properly. But when I make publish, in the decompiler I see that Host is equal to "http://localhost:63372/" string.
Where is a problem?
Have you tried:
#if (DEBUG)
MySettings.Default.Host = "http://localhost:63372/";
#endif
#if (!DEBUG)
MySettings.Default.Host = "http://example.com/";
#endif
I have found the anwser. When you publish with clickOnce it uses current selected mode. So, I need to switch to release and then click Publish.
This question already has answers here:
How do I check the active solution configuration Visual Studio built with at runtime?
(4 answers)
Closed 9 years ago.
I have a C# windows application with 4 App Modes - Debug,Pre-Release, Release and UAT. I have to display in the footer of my main form as to what is my current Operating mode. Any idea how I can retrieve the same?
You could use C#'s #if directive http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
string mode;
#if DEBUG
mode = "DEBUG";
#elseif RELEASE
mode = "RELEASE";
#else
mode = "UAT";
#end
You would also need to set up the symbol in the project file so that the code can pick up on it. You'll find it by selecting the project properties. (Select the project file and press Alt+Enter)
Visual Studio doesn't provide for applying the configuration names to your .NET code.
However, you may want to define a custom symbol for each of your configurations in your project build settings and query these symbols in your code, like this:
#if DEBUG
...
#elif PRERELEASE
...
#elif RELEASE
...
#elif UAT
...
#endif
you must use #if reference.
check http://msdn.microsoft.com/tr-tr/library/4y6tbswk.aspx
how can i configure my Visual Studio 2010 C# solution/project so
that when i select a Debug configuration - ConnectionString#1 would be used
Release - Connection string #2
and
"Myconfiguarion1" (which was copied from debug) -> Connection string #3
I got to it work with debug in such a way:
if (ConfigurationManager.ConnectionStrings["ConnectionString1"] != null)
{
winApplication.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
}
#if DEBUG
if(ConfigurationManager.ConnectionStrings["ConnectionString2"] != null)
{
winApplication.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
}
#endif
but this doesn't work with "MybuildConfiguration"
If you're trying to do this for the web.config file of an ASP.NET project in Visual Studio 2010, it's built-in via XML Transformations for web.config.
Web Deployment: Web.Config Transformations
If you're trying to do this for an app.config file, you can use the same transformations but working with them is a bit of a hack:
Visual Studio App.config XML Transformations
Both boil down to actually using separate config files for the different environments you are going to be running your app in. That allows you to supply different values for any of the keys based on what environment you're running in.
I think you can use conditional compilation constants. To define them,
you have to open project property window, select compilation tab, and define a name in the conditional constants field, e.g. CONN1.
This constants get defined only for your active build configuration, so you can define CONN1 for Debug configuration,CONN2 for Release configuration,CONN3 for your custom configuration etc.
then, in your code, you can use:
#ifdef CONN1
//use connection 1
#else
#ifdef CONN2
//use connection 2
#else
//use connection 3
#endif
#endif
I'm using Visual c# Express 2008. I want to have a special command executed just in "Release" builds - this command should not be executed while I'm creating and running Debug versions. Is it possible to implement code depending on my build-type (Debug or. Release)?
For example:
if(??buildtype?? == "Release")
{
//... special command ...
MessageBox.Show("RELEASE version");
}
else
{
//... normal command ...
MessageBox.Show("debug release");
}
#if DEBUG
// Commands that should run in debug builds.
#else
// Commands that should run in release builds.
#endif