How to obtain system environment variables in C #

Source: Internet
Author: User

Environment class is required to obtain system environment variables in C. It provides information about the current environment and platform as well as methods for operating them. This class cannot be inherited.

 

Run the following code to get the value of % systemdrive %, that is, "C :"

String Spath = environment. getenvironmentvariable ("systemdrive ")

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

The following is the sample code of Environment class on msdn.

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

The following code lists the environment information of the current system.

// Sample for Environment class summaryusing 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: ");    IDictionaryenvironmentVariables = 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 TEXTGetCommandLineArgs: env0, ARBITRARY, TEXTCurrentDirectory: C:\Documents and Settings\!---OMITTED---!ExitCode: 0HasShutdownStarted: FalseMachineName: !---OMITTED---!NewLine:  first line  second line  third lineOSVersion: Microsoft Windows NT 5.1.2600.0StackTrace: '   at System.Environment.GetStackTrace(Exception e)   at System.Environment.GetStackTrace(Exception e)   at System.Environment.get_StackTrace()   at Sample.Main()'SystemDirectory: C:\WINNT\System32TickCount: 17995355UserDomainName: !---OMITTED---!UserInteractive: TrueUserName: !---OMITTED---!Version: !---OMITTED---!WorkingSet: 5038080ExpandEnvironmentVariables:  My system drive is C: and my system root is C:\WINNTGetEnvironmentVariable:  My temporary directory is C:\DOCUME~1\!---OMITTED---!\LOCALS~1\Temp.GetEnvironmentVariables:   !---OMITTED---!GetFolderPath: C:\WINNT\System32GetLogicalDrives: A:\, C:\, D:\*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.