您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> .net编程 >> 正文

C#获取系统环境变量

作者:佚名    责任编辑:admin    更新时间:2022-06-22

class="area">

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace test2
{
class Program
{
static void Main(string[] args)
{
// 把环境变量中所有的值取出来,放到变量environment中
IDictionary environment = Environment.GetEnvironmentVariables();

// 打印表头
Console.WriteLine( "环境变量名\t=\t环境变量值 ");

// 遍历environment中所有键值
foreach (string environmentKey in environment.Keys)
{
// 打印出所有环境变量的名称和值
Console.WriteLine( "{0}\t=\t{1} ", environmentKey, environment[environmentKey].ToString());
}
Console.Read();
}
}
}