Application built mode in c# [duplicate] - c#

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

Related

Detect when WPF/WInForm/Android runtime is included in a project

I have an Extension class I share within multiple projects where the project can be Android (Xamarin/.NET MAUI) but also a WinForm/WPF app or even a WinForm one with some WPF windows and I'm wondering if there are existing preprocessor macros to enable or disable some code, e.g. 'System.Data.SqlClient' stuff is not available on Android and 'System.Windows' (which I have some extensions) is not part of a WinForm project unless some dll are manually referenced (such PresentationCore, PresentationFramework, WindowsBase, WindowsFormsIntegration etc.). So my question is:
is there any preprocessor macro to enable/disable code for Android, WPF and WinForm at compile time?
what i would is something like:
#if !(ANDROID_RUNTIME)
public static string SqlDataReaderStringExt(this SqlDataReader reader, int i)
{
...
}
#endif
#if WPF_RUNTIME
....
#endif
#if WINFORM_RUNTIME
....
#endif
so, stop manually comment the code in each project.

.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

Current solution contains incorrect configurations mapping using Premake5 with C#

I'm trying to develop an engine and I was looking for a GUI library in C# in order to build the editor for my engine. I found Avalonia but I'm having some problems setting up the whole environment.
I'm using Premake5 as build tool mixing C++ and C# but I think the problem here is not the languages mixing.
I'm getting this error when I generate my visual studio solution file. Sorry about the image, I needed to post it that way cause when I press the "Open Configuration Manager" the error is gone and exit the Configuration Manager window the compilation works as expected, quite weird.
Here's my code:
This is the premake5 script I run:
include "Dependencies.lua"
workspace "LeafEngine"
startproject "LeafEditor"
configurations { "Debug", "Release" }
platforms { "x64" }
flags { "MultiProcessorCompile" }
outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.platform}"
group "Dependencies"
include "Leaf/vendor/glfw"
include "Leaf/vendor/imgui"
group ""
include "Leaf"
include "LeafEditor"
include "LeafGame"
Leaf is my C++ engine and LeafGame just a C++ test. Leaf editor is the C# project, which looks like this:
project "LeafEditor"
kind "WindowedApp"
language "C#"
clr "On"
targetdir ("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}")
objdir ("%{wks.location}/bin-obj/" .. outputdir .. "/%{prj.name}")
dotnetframework "net6.0"
nuget { "Avalonia:0.10.13", "Avalonia.Desktop:0.10.13",
"Avalonia.Diagnostics:0.10.13", "Avalonia.ReactiveUI:0.10.13",
"XamlNameReferenceGenerator:1.3.4"
}
files
{
"src/**.cs",
"src/**.xaml",
"src/**.xaml.cs",
}
links
{
"Microsoft.CodeAnalysis.CSharp.NetAnalyzers",
"Microsoft.CodeAnalysis.NetAnalyzers",
"System.Text.Json.SourceGeneration",
"Microsoft.NETCore.APP",
}
filter "system:Windows"
defines "LF_WINDOWS"
filter "system:Unix"
defines "LF_LINUX"
filter "configurations:Debug"
defines "LF_DEBUG"
runtime "Debug"
symbols "on"
filter "configurations:Release"
defines "LF_RELEASE"
runtime "Release"
optimize "full"
Another curious thing about Avalonia: as you can see I only have one available platform ("x64") for building. Well, Avalonia compiles with ("Any CPU") platform and that also breaks my whole building set up. Besides, Avalonia gets compiled with Any CPU when I load the project not when I compile the project, is that right?
Thanks in advance, this error is killling me.
I just had the same issue, both when my C# project was built with premake and with it as an external project.
The solution (granted I gave up on premake for C# so this may only apply to externalproject) is to not add architecture or any specific platform to your workspace, and instead add it to all but your C# project. I would leave a comment since this might not be the answer you're looking for, but I don't have enough reputation. Anyway, for example,
workspace "Lotus"
startproject "LotusEditor"
configurations
{
"Debug",
"Release"
}
project "CppProject"
architecture "x86_64" -- sets CppProject as x64 but not the workspace
-- more project config
externalproject "CsProject" -- no architecture/platform set for this. Might work for non externalprojects too
location "path"
uuid "insert uuid"
kind "WindowedApp"
language "C#"
Even with my project made manually not with premake, and set with target platform as x64, it still seems to want "Any CPU" as the configuration, which premake can't do.

Running NUnit test depending on a condition

I'm writing a project using Win7 x64. Some part of my tests requires using SQLServer CE which only represents support for x86. I'm using Visual Studio 2010 Express and I gotta change platform target for my projects manually editing *.cproj files to run, for example, schema export test (NHibernate). Howcome I run a part of my tests depending on a platform target.
Thanks!
I don't know if there's a built-in mechanism in NUnit to handle this scenario, but at the very least you can use preprocessor directives.
For instance, create a "Debug x86" solution configuration, targeting x86. Then define the DEBUG_X86 conditional compilation symbol (in the properties of the project). Finally, surround your unit test with preprocessor directives:
#if DEBUG_X86
[Test]
public void Test()
{
// This test will only run when compiled with Debug x86
}
#endif
Edit: Actually, you don't even have to create a new solution configuration, as it's possible to define the conditional symbols depending on the platform (https://stackoverflow.com/a/1313450/869621). So define a WIN32 compilation symbol, and surround your test with it:
#if WIN32
[Test]
public void Test()
{
// This test will only run when compiled for x86
}
#endif

Different configuaration string for specific build configuration

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

Categories