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 ...
随机推荐
- 【Swift】学习笔记(四)——设置(Collection)
Swift和其他语言也提供了两种类型的集合:数组和字典 数组:数组用来按顺序存储同样类型的数据,swift规定它是类型安全的,每个数组都有自己的类型也就是其它语言所说的泛型. 创建数组: 1.var ...
- Mybatis之ResultMap一个简短的引论,关联对象
基础部分能够查看我的还有一篇博客http://blog.csdn.net/elim168/article/details/40622491 MyBatis中在查询进行select映射的时候.返回类型能 ...
- 遗传算法解决旅行商问题(TSP)
这次的文章是以一份报告的形式贴上来,代码只是简单实现,难免有漏洞,比如循环输入的控制条件,说是要求输入1,只要输入非0就行.希望会帮到以后的同学(*^-^*) 一.问题描述 旅行商问题(Traveli ...
- Java的进程内缓存框架:EhCache (转)
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种缓存 ...
- 使用C#在Windows应用商店程序中获取CPU信息
using Windows.Devices.Enumeration; string guidStr="{97FADB10-4E33-40AE-359C-8BEF029DBDD0}" ...
- HDU 3313 Key Vertex(dfs + bfs)
HDU 3313 Key Vertex 题目链接 题意:一个有向无环图.求s,t之间的割点 思路:先spfa找一条最短路出来,假设不存在.就n个都是割点. 然后每次从s进行dfs,找到能经过最短路上的 ...
- transform:translateZ() 字体模糊问题 父类重返Z轴平面
translateZ()变糊 第一种情况: 当translateZ(m)中的 m设置为 非整数,1.5px 之类的,字体会模糊,但是不明显;和浏览器渲染,字体格式,或者操作系统有关, 这个 css中 ...
- mysql函数二
四.条件推断函数 1.if(expr,v1,v2)函数:成立返回结果v1,否则结果v2 例:select id,if(grade>=60,'pass','fail') from t; 2.IFN ...
- HOWTO: 为GitHub for Windows指定代理服务器(转)
If the command line way of configuring your proxy server doesn't work, you can probably just edit .g ...
- 在SurfaceView中自由书写和擦除
/////////继承SurfaceView 的类 public class PaintView extends SurfaceView implements Runnable,SurfaceHold ...