✅ How to read Environment Variables in VS Code ?
Edit. Solved. Code was correct, but had to run from ".NET Core Launch (Console)" and not from "Debug project associated with this file"
Using .Net 8.0.402 and Visual Studio Code 1.94.0
I'm trying to get pretty simply a value from a file named launch.json in .vscode folder. The path is:
.vscode\launch.json
and the path of the file I'm calling from is:
Program.cs
Program.cs has:
private static void Main(string[] args)
{
string a = Environment.GetEnvironmentVariable("API_ENDPOINT");
And the launch.json has:
{
"version": "0.2.0",
"configurations": [
{
...
"env": {
"API_ENDPOINT": "europe-west2-aiplatform.googleapis.com",
And string a gets a value of null. What's the correct way to receive it?
I feel like this is something very, very simple to solve.
4 Replies
Environment.GetEnvironmentVariable(API_ENDPOINT)
is not valid code, unless API_ENDPOINT
is a variable somewhere
Also, are you running the program through VSC, or with dotnet run
in the console?From VSC.
I changed it now to be:
string a = Environment.GetEnvironmentVariable("API_ENDPOINT");
But the result is the same. If I try to get the list of the keys, there's 60 of those, so it does get something, but I still don't know how to refer to the "API_ENDPOINT".
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Got help from a co-worker. So yeah, the code is correct. I just need to run it thru ".NET Core Launch (console)" and not from "Debug project associated with this file".