Compiler directive (DEBUG) work wrong for publishing - c#

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.

Related

.NET MAUI Environment variable

Is there a way how to find out that the current environment in .NET MAUI is development ?
Because before in my Blazor WASM I did just:
builder.HostEnvironment.IsDevelopment()
But in MauiAppBuilder I dont see any environment property.
Well what I usually do for this is I use the if directive
So for instance if I wanna check if wanna set a different value to var in release than in debug I would do something like below :
#if DEBUG
var a = "debug";
#elif RELEASE
var a = "release"
#endif
Good luck hope this helps

Detect DEBUG, RELEASE and my own mode C#

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.

Always running #if debug in dll

I have a property in config named to class like the following in my framework project:
public static string PropertyName
{
get
{
#if DEBUG
return GetValue("TestDevelopment");
#else
return GetValue("Test");
#endif
}
}
I've compiled my project. And I added this config.dll file to different project. But always returning GetValue("TestDevelopment") from this dll. I 've compiled Release mode but it's not working.
What's the problem to be? I hope, could tell...
If you compiled the dll as debug, and copied the file to a different one, it'll always be whatever it was when you compiled it.
You'll have to compile it in Release for it to be in release.

Application built mode in c# [duplicate]

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

code depending on build-type?

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

Categories