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#中如何获取系统环境变量的更多相关文章
- C#中如何获取系统环境变量等
C#中获取系统环境变量需要用到Environment 类. 其中提供了有关当前环境和平台的信息以及操作它们的方法.该类不能被继承 以下代码得到%systemdrive%的值,即“C:” string ...
- Springboot yml获取系统环境变量的值
注意,这里说的是获取系统环境变量的值,譬如Windows里配置的JAVA_HOME之类的,可以直接在Springboot的配置文件中获取. 我们经常使用一些docker管理平台,如DaoCloud.r ...
- Springboot配置文件获取系统环境变量的值
注意,这里说的是获取系统环境变量的值,譬如Windows里配置的JAVA_HOME之类的,可以直接在Springboot的配置文件中获取. 我们经常使用一些docker管理平台,如DaoCloud.r ...
- python 获取系统环境变量 os.environ and os.putenv
从一段code说起 “if "BATCH_CONFIG_INI" in os.environ:” 判断环境变量的值有没有定义 如果定义的话就去环境变量的值,否则就取当前目录下的co ...
- JAVA 获取系统环境变量
分享代码: package com.base.entity; import java.io.Serializable; import java.util.Comparator; /** * 系统环境变 ...
- Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法
系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> ...
- C#设置和获取系统环境变量
C#设置和获取环境变量 1.前言 本来想拿学校机房的Android编辑器直接粘到自己电脑上用,发现它的eclipse是 32位的,而我的JDK是64位的,于是想到干脆装两个JDK,用C#做一个能够更改 ...
- InstallSheild 获取系统环境变量,如Desktop路径等
使用FOLDER_DESKTOP变量获取的桌面路径可能为:C:\Users\Public\Desktop 而不是C:\Users\用户\Desktop Copy and paste the follo ...
- C#中如何获取系统文件及操作系统的环境变量等
C#中获取系统环境变量需要用到Environment 类. 其中提供了有关当前环境和平台的信息以及操作它们的方法.该类不能被继承 以下代码得到%systemdrive%的值,即“C:” string ...
随机推荐
- 设计模式之前奏(UML类图)
原文:设计模式之前奏(UML类图) 本人菜菜一个,最近一直在博客园游走闲逛,看到了各种技术,各种各种…….便看到了大话设计模式这本书,下了电子版的看了看第一章,感觉相当不错,不仅通俗易懂,而且与实际案 ...
- 好大滴坑, Spring MVC覆盖了Trsaction
好大滴坑. Spring MVC覆盖了Trsaction 解决方式: <!-- package-scan 4 Spring MVC --> <context:component-sc ...
- poj3903 Stock Exchange(最长上升子序列)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3903">http://poj.org/problem?id=3903 Descrip ...
- 惊人go语言(image网站开发)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 有过python web开发经验的朋友.相信对它的便利性肯定印象很深刻. 事实上利用go语言对 ...
- SharePoint 2013 禁用搜索服务
原文:SharePoint 2013 禁用搜索服务 前言,在SharePoint2013中,对于硬件需求的提升,让我们虚机里安装总是一筹莫展,尤其开启了搜索服务以后,对于内存的消耗就更加严重,尤其对于 ...
- poj 2565 Ants (KM+思维)
Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4125 Accepted: 1258 Special Ju ...
- Building a RESTful Web Service(转)
Building a RESTful Web Service This guide walks you through the process of creating a "hello wo ...
- Linux核心设计依据(七)系统调用
我理解的系统调用,用户进程和内核是内核提供了一个接口进行交互.除了异常和下降外.内核系统调用是唯一合法入境.像/proc还通过系统调用访问. 系统调用的意义: 让用户进程受限地訪问硬件设备 为用户空间 ...
- 乘法逆元...Orz
最近打的几场比赛,都出现了有关逆元的题目,今天就整理了一下... 求乘法逆元的几种方法:http://www.cnblogs.com/james47/p/3871782.html 博文转载链接:htt ...
- jsScript中的一些操作方法
1.采用dom方式对script标签进行操作 var h = document.getElementsByTagName('HEAD').item(0); var s = document.creat ...