文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#中如何获取系统环境变量

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

时间:2011-01-26  来源:Pegasus923

 

以下语句得到%systemdrive%的值,即“C:”

string sPath = Environment.GetEnvironmentVariable("systemdrive")

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

 

以下是MSDN上Environment Class的Sample code。

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

 

Sample for Environment class summary
  1 using System;
2 using System.Collections;
3
4 class Sample
5 {
6 public static void Main()
7 {
8 String str;
9 String nl = Environment.NewLine;
10 //
11 Console.WriteLine();
12 Console.WriteLine("-- Environment members --");
13
14 // Invoke this sample with an arbitrary set of command line arguments.
15 Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
16
17 String[] arguments = Environment.GetCommandLineArgs();
18 Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
19
20 // <-- Keep this information secure! -->
21 Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);
22
23 Console.WriteLine("ExitCode: {0}", Environment.ExitCode);
24
25 Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);
26
27 // <-- Keep this information secure! -->
28 Console.WriteLine("MachineName: {0}", Environment.MachineName);
29
30 Console.WriteLine("NewLine: {0} first line{0} second line{0} third line",
31 Environment.NewLine);
32
33 Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
34
35 Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
36
37 // <-- Keep this information secure! -->
38 Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);
39
40 Console.WriteLine("TickCount: {0}", Environment.TickCount);
41
42 // <-- Keep this information secure! -->
43 Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);
44
45 Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);
46
47 // <-- Keep this information secure! -->
48 Console.WriteLine("UserName: {0}", Environment.UserName);
49
50 Console.WriteLine("Version: {0}", Environment.Version.ToString());
51
52 Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);
53
54 // No example for Exit(exitCode) because doing so would terminate this example.
55
56 // <-- Keep this information secure! -->
57 String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
58 str = Environment.ExpandEnvironmentVariables(query);
59 Console.WriteLine("ExpandEnvironmentVariables: {0} {1}", nl, str);
60
61 Console.WriteLine("GetEnvironmentVariable: {0} My temporary directory is {1}.", nl,
62 Environment.GetEnvironmentVariable("TEMP"));
63
64 Console.WriteLine("GetEnvironmentVariables: ");
65 IDictionary environmentVariables = Environment.GetEnvironmentVariables();
66 foreach (DictionaryEntry de in environmentVariables)
67 {
68 Console.WriteLine(" {0} = {1}", de.Key, de.Value);
69 }
70
71 Console.WriteLine("GetFolderPath: {0}",
72 Environment.GetFolderPath(Environment.SpecialFolder.System));
73
74 String[] drives = Environment.GetLogicalDrives();
75 Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
76 }
77 }
78 /*
79 This example produces results similar to the following:
80 (Any result that is lengthy or reveals information that should remain
81 secure has been omitted and marked "!---OMITTED---!".)
82
83 C:\>env0 ARBITRARY TEXT
84
85 -- Environment members --
86 CommandLine: env0 ARBITRARY TEXT
87 GetCommandLineArgs: env0, ARBITRARY, TEXT
88 CurrentDirectory: C:\Documents and Settings\!---OMITTED---!
89 ExitCode: 0
90 HasShutdownStarted: False
91 MachineName: !---OMITTED---!
92 NewLine:
93 first line
94 second line
95 third line
96 OSVersion: Microsoft Windows NT 5.1.2600.0
97 StackTrace: ' at System.Environment.GetStackTrace(Exception e)
98 at System.Environment.GetStackTrace(Exception e)
99 at System.Environment.get_StackTrace()
100 at Sample.Main()'
101 SystemDirectory: C:\WINNT\System32
102 TickCount: 17995355
103 UserDomainName: !---OMITTED---!
104 UserInteractive: True
105 UserName: !---OMITTED---!
106 Version: !---OMITTED---!
107 WorkingSet: 5038080
108 ExpandEnvironmentVariables:
109 My system drive is C: and my system root is C:\WINNT
110 GetEnvironmentVariable:
111 My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.
112 GetEnvironmentVariables:
113 !---OMITTED---!
114 GetFolderPath: C:\WINNT\System32
115 GetLogicalDrives: A:\, C:\, D:\
116
117 */

 

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载