原文:C#中如何获取系统环境变量

C#中获取系统环境变量需要用到Environment Class。其中提供了有关当前环境和平台的信息以及操作它们的方法。该类不能被继承。

以下代码得到%systemdrive%的值,即“C:”

string sPath = Environment.GetEnvironmentVariable("systemdrive")

string sPath = Environment.GetEnvironmentVariable("systemdrive");
Console.WriteLine(sPath); //C:

以下是MSDN上Environment Class的Sample code。

http://msdn.microsoft.com/en-us/library/system.environment.aspx

以下代码列出了当前系统的环境信息。

// Sample for Environment class summary
using System;
using System.Collections; class Sample
{
public static void Main()
{
String str;
String nl = Environment.NewLine;
//
Console.WriteLine();
Console.WriteLine("-- Environment members --"); // Invoke this sample with an arbitrary set of command line arguments.
Console.WriteLine("CommandLine: {0}", Environment.CommandLine); String[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments)); // <-- Keep this information secure! -->
Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory); Console.WriteLine("ExitCode: {0}", Environment.ExitCode); Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted); // <-- Keep this information secure! -->
Console.WriteLine("MachineName: {0}", Environment.MachineName); Console.WriteLine("NewLine: {0} first line{0} second line{0} third line",
Environment.NewLine); Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString()); Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace); // <-- Keep this information secure! -->
Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory); Console.WriteLine("TickCount: {0}", Environment.TickCount); // <-- Keep this information secure! -->
Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName); Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive); // <-- Keep this information secure! -->
Console.WriteLine("UserName: {0}", Environment.UserName); Console.WriteLine("Version: {0}", Environment.Version.ToString()); Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet); // No example for Exit(exitCode) because doing so would terminate this example. // <-- Keep this information secure! -->
String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
str = Environment.ExpandEnvironmentVariables(query);
Console.WriteLine("ExpandEnvironmentVariables: {0} {1}", nl, str); Console.WriteLine("GetEnvironmentVariable: {0} My temporary directory is {1}.", nl,
Environment.GetEnvironmentVariable("TEMP")); Console.WriteLine("GetEnvironmentVariables: ");
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry de in environmentVariables)
{
Console.WriteLine(" {0} = {1}", de.Key, de.Value);
} Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.System)); String[] drives = Environment.GetLogicalDrives();
Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
}
}
/*
This example produces results similar to the following:
(Any result that is lengthy or reveals information that should remain
secure has been omitted and marked "!---OMITTED---!".) C:\>env0 ARBITRARY TEXT -- Environment members --
CommandLine: env0 ARBITRARY TEXT
GetCommandLineArgs: env0, ARBITRARY, TEXT
CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
ExitCode: 0
HasShutdownStarted: False
MachineName: !---OMITTED---!
NewLine:
first line
second line
third line
OSVersion: Microsoft Windows NT 5.1.2600.0
StackTrace: ' at System.Environment.GetStackTrace(Exception e)
at System.Environment.GetStackTrace(Exception e)
at System.Environment.get_StackTrace()
at Sample.Main()'
SystemDirectory: C:\WINNT\System32
TickCount: 17995355
UserDomainName: !---OMITTED---!
UserInteractive: True
UserName: !---OMITTED---!
Version: !---OMITTED---!
WorkingSet: 5038080
ExpandEnvironmentVariables:
My system drive is C: and my system root is C:\WINNT
GetEnvironmentVariable:
My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
GetEnvironmentVariables:
!---OMITTED---!
GetFolderPath: C:\WINNT\System32
GetLogicalDrives: A:\, C:\, D:\ */

C#中如何获取系统环境变量的更多相关文章

  1. C#中如何获取系统环境变量等

    C#中获取系统环境变量需要用到Environment 类. 其中提供了有关当前环境和平台的信息以及操作它们的方法.该类不能被继承 以下代码得到%systemdrive%的值,即“C:” string ...

  2. Springboot yml获取系统环境变量的值

    注意,这里说的是获取系统环境变量的值,譬如Windows里配置的JAVA_HOME之类的,可以直接在Springboot的配置文件中获取. 我们经常使用一些docker管理平台,如DaoCloud.r ...

  3. Springboot配置文件获取系统环境变量的值

    注意,这里说的是获取系统环境变量的值,譬如Windows里配置的JAVA_HOME之类的,可以直接在Springboot的配置文件中获取. 我们经常使用一些docker管理平台,如DaoCloud.r ...

  4. python 获取系统环境变量 os.environ and os.putenv

    从一段code说起 “if "BATCH_CONFIG_INI" in os.environ:” 判断环境变量的值有没有定义 如果定义的话就去环境变量的值,否则就取当前目录下的co ...

  5. JAVA 获取系统环境变量

    分享代码: package com.base.entity; import java.io.Serializable; import java.util.Comparator; /** * 系统环境变 ...

  6. Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法

    系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> ...

  7. C#设置和获取系统环境变量

    C#设置和获取环境变量 1.前言 本来想拿学校机房的Android编辑器直接粘到自己电脑上用,发现它的eclipse是 32位的,而我的JDK是64位的,于是想到干脆装两个JDK,用C#做一个能够更改 ...

  8. InstallSheild 获取系统环境变量,如Desktop路径等

    使用FOLDER_DESKTOP变量获取的桌面路径可能为:C:\Users\Public\Desktop 而不是C:\Users\用户\Desktop Copy and paste the follo ...

  9. C#中如何获取系统文件及操作系统的环境变量等

    C#中获取系统环境变量需要用到Environment 类. 其中提供了有关当前环境和平台的信息以及操作它们的方法.该类不能被继承 以下代码得到%systemdrive%的值,即“C:” string ...

随机推荐

  1. [Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]

    原文:[Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]  我的文章一定要做到对读者负责,否则就是失败的文章  -- ...

  2. System.ComponentModel.BackgroundWorker在WinForm中的异步使用

    为了防止操作过程中界面卡死,和WinForm搭配最适合的就是BackgroundWorker了.BackgroundWorker 类 using System; using System.Compon ...

  3. ORA-38760: This database instance failed to turn on flashback database 第三篇

    ORA-38760: This database instance failed to turn on flashback database  第三篇 第一篇 第二篇 问题现象:      在数据库a ...

  4. poj3281-Dining ,最大流量,内置图

    id=3281">点击打开链接 分析: 求最大流 建图: 拆点 牛拆成左边与食物相连的左牛 和 右边与饮料相连的右牛 1.s->食物 连边 2.食物->左牛 3.左牛-&g ...

  5. android visible invisible和gone差异

    android中UI应用的开发中常常会使用view.setVisibility()来设置控件的可见性.当中该函数有3个可选值.他们有着不同的含义: View.VISIBLE--->可见 View ...

  6. Android CTS測试Fail项改动总结(四)

    Android5.1上的測试 1.android.security.cts.SELinuxDomainTest# testInitDomain fail 打印的log junit.framework. ...

  7. 实现android里面WebView显示内容

    在日常学习.我们会看到,当手机应该检查注册协议.单击协议时,以查看内部协议的全部内容! 以下给大家看看实现的过程: 首先贴show_xy.XML代码: <WebView android:layo ...

  8. Windows下 C++ WT +VS2013配置

    引出 最近在学习使用C++,另外对建站有点兴趣,所以就找到了WT.对于WT的详细介绍,这里不讲,直接看官网就好. 此文为本人原创,转载请注明出处. 先丢出官网上的干货: WT官方网站: https:/ ...

  9. Mac周边环境 goBASIC语言HelloWorld

    1. 安装mercurial Mercurial 是一种轻量级分布式版本号控制系统,採用 Python 语言实现 能够输入hg命令查询系统是否安装mercurial,能够例如以下两种命令安装 $sud ...

  10. 变化Android系统属性SystemProperties.set(&quot;sys.powerctl&quot;, &quot;shutdown&quot;)关机分析

    基本介绍: 从以前的博客中提到,我们,最后,通过关机过程变化Android关机属性(SystemProperties.java由JNI呼叫接入系统属性),当然,我们也能adb命令变化Android系统 ...