获取计算机运行环境等信息(System.Evnironment类)

通过WMI我们可以获取到计算机的软件信息,.NET Framework也提供了一个类来获取这些信息,这就是System.Evnironment类
http://msdn.microsoft.com/zh-cn/library/system.environment(VS.80).aspx 给出了具体说明,抄过来以便查询
Environment 类

提供有关当前环境和平台的信息以及操作它们的方法。无法继承此类。


C# 复制代码 
//  Sample for Environment class summary
using  System;
using  System.Collections;

class  Sample 
ExpandedBlockStart.gifContractedBlock.gif
{
    
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
    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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        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));
    }

}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
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:\

*/

转载于:https://www.cnblogs.com/forrestsun/articles/1578867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值