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

系统 3733 0

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#中如何获取系统环境变量


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论