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 ...
随机推荐
- 解决ASP.NET Web API Json对象循环参考错误
前言 一般我们在开法 ASP.NET Web API 时,如果是使用 Entity Framework 技术来操作数据库的话,当两个 Entity 之间包含导览属性(Navigation Proper ...
- MemCache分布式内存对象缓存系统
MemCache超详细解读 MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而 ...
- D3D 扎带 小样本
D3D 符合基本程序 #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.li ...
- COM Interop
1.MSDN上的文章:COM Interop教程 2.接口的三种类型:IDispatch.IUnknown和Dual 3.使用TlbImp来更灵活地自动生成RCW 4.托管事件基于委托,而非托管事件( ...
- freemarker定义自己的标签错误(八)
1.错误叙述性说明 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgrError: Unknow ...
- 经验36--C#无名(大事,物...)
有时候,方便代码,它会使用匿名的东西. 1.匿名事件 args.CookieGot += (s, e) => { this ...
- ecshop广告调用方法
在简单地概括ecshop广告调用该方法,已发表在博客上,在这里,我们总结了以下 :就是官方默认的方法.先加入广告位,然后加入模板的广告位区域,再在将两者相应上. 1.后台 > 广告管理 > ...
- Advanced Installer 9.8打包实录
原文 Advanced Installer 9.8打包实录 主要介绍:(1)创建工程,(2)创建快捷方式及其图标(3)卸载设置 创建工程(.net为例): 工程创建完成....接下来进行简单设置 开始 ...
- 关于在 xmlSPY 出现的错误 DOCTYPE-EXternalID的名称必须既是SYSTEM 又是PUBLIC?(转)
最近我在做学习xml时,遇见一个问题,我本用的是2009 xml spy后来老是出现问题 ,就是不能通过,后来我上网查了一下,发现是以一问题,不管是在2006中还是在2009中,都会出现这样的问题,要 ...
- DEV GridView嵌套
近来的DEV搞更多.试图寻找专业点的程序做,对这样一个小小的研究. 本篇是多么真实,现在的记录,可以通过点击这条线的子表的内容相关联的行中打开的列表.的影响,如下面的: 以下是实现过程: 1.设计器里 ...