How to get the environment variables in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] environmentVariables =
            {
                "%WINDIR%",
                "%HOMEDRIVE%",
                "%USERNAME%",
                "%COMPUTERNAME%"
            };

            foreach (string s in environmentVariables)
            {
                string content = Environment.ExpandEnvironmentVariables(s);
                Console.WriteLine(content);
            }
        }
    }
}

/*
run:
  
C:\Windows
C:
User
USER-PC

*/



 



answered Jan 13, 2017 by avibootz

Related questions

2 answers 138 views
1 answer 139 views
2 answers 175 views
175 views asked May 4, 2021 by avibootz
1 answer 145 views
145 views asked Aug 24, 2020 by avibootz
2 answers 123 views
123 views asked Nov 18, 2023 by avibootz
1 answer 173 views
1 answer 175 views
...