Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Environment Variable in csproj file?

I want to get the environment variable in csproj, because there I have a condition which exclude appsettings from publish.

enter image description here enter image description here
I want this because, my appsettings didn't depends to Solution Configuration, them depends only from environment variables.

Instead of '$(Configuration)' != Debug' I want something like 'envVariable != Development' etc.

Or is it another method to exclude those files regarding to env variables?

in C# is this method: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ").

like image 443
Alex Avatar asked Feb 05 '18 08:02

Alex


People also ask

How do I view Csproj file?

How to open a CSPROJ file. CSPROJ files are are meant to be opened and edited in Microsoft Visual Studio (Windows, Mac) as part of Visual Studio projects. However, because CSPROJ files are XML files, you can open and edit them in any text or source code editor.

Where are Environment Variables stored Visual Studio?

In Visual Studio 2019 right-click your project, choose Properties . In the project properties window, select the Debug tab. Then, under Environment variables change the value of your environment from Development to Production or other environments.

How do I view Environment Variables in Visual Studio?

In Visual Studio, we can set ASPNETCORE_ENVIRONMENT in the debug tab of project properties. Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below.

How do I set Environment Variables in MSBuild?

Click on System and Security and then on System. In the left pane, click on Advanced system settings. At the very bottom of the pop up, click on Environment Variables. Edit the Path variable and append the folder's path that contains the MSBuild.exe to it (e.g., ;C:\Windows\Microsoft.NET\Framework64\v4.


Video Answer


1 Answers

So I ran into this same problem today and got it working rather easily. This was one of the first relevant results on google when I searched for this so thought I'd share.

Actually the $() operator is used to resolve any variable within the .csproj but it's also seeded with environment variables already when MSBuild is triggered. So in your case you could do $(envVariable) or $(ASPNETCORE_ENVIRONMENT).

They're brought in like any other .csproj variable.

like image 120
Barak Gall Avatar answered Nov 15 '22 19:11

Barak Gall