1

I have a few setting like the database-connection that differ between my test- and live-system. Has Visual Studio a feature that determines automatically which settings to use by just pulling one switch somewhere? I don't want to use #if DEBUG because i still want to be able to debug my live-system before i publish it. Is there no better way then just to use a bool?

  • 1
    What about using a configuration file that you can swap? – PMF Jun 23 '23 at 13:47
  • What does "better" mean? You can't get much simpler that a single bit... – Heretic Monkey Jun 23 '23 at 14:05
  • 2
    Have a look at [this post](https://stackoverflow.com/questions/46364293/automatically-set-appsettings-json-for-dev-and-release-environments-in-asp-net-c). – Mustafa Özçetin Jun 23 '23 at 14:23
  • 1
    First of all: What type of Project are we talking about and what dotnet Version is in use? Answers may vary vastly depending on whether this is cloud-related, a desktop app in WinForms ... – Fildor Jun 23 '23 at 14:32
  • @Fildor-standswithMods it's a blazor server app + class libraries in ASP.NET core 6.0 – cattle81194 Jun 26 '23 at 08:57
  • @HereticMonkey my question was just if there's a build in feature of VS that makes switching between variables before build easy. that would be the recommended way, no? if not, i'm just gonna use a const bool – cattle81194 Jun 26 '23 at 09:17

2 Answers2

-1

The best way to make changes based on environment is using environment variables For example you can put a variable name “environmentName” and put its value as develop,prod,stage or any other

Then you can pull it and preform different actions based on it

String env = Environment.GetEnvironmentVariable("environmentName");

-1

Id recommend environment variables from personal experience.

See here for a simple explanation: https://mahmudx.com/how-to-store-and-retrieve-environment-variable-in-csharp