Tải bản đầy đủ - 0 (trang)
14-1. Access Runtime Environment Information

14-1. Access Runtime Environment Information

Tải bản đầy đủ - 0trang

CHAPTER 14 ■ WINDOWS INTEGRATION



Member



Description



TickCount



Gets an int representing the number of milliseconds that have elapsed

since the system was started.



UserDomainName



Gets a string containing the Windows domain name to which the

current user belongs. This will be the same as MachineName if the user has

logged in on a machine account instead of a domain account.



UserInteractive



Gets a bool indicating whether the application is running in user

interactive mode; in other words, its forms and message boxes will be

visible to the logged-on user. UserInteractive will return false when the

application is running as a service or is a web application.



UserName



Gets a string containing the name of the user that started the current

thread, which can be different from the logged-on user in case of

impersonation.



Version



Gets a System.Version object that contains information about the

version of the CLR.



Methods

ExpandEnvironmentVariables



Replaces the names of environment variables in a string with the value

of the variable. See recipe 14-2 for details.



GetCommandLineArgs



Returns a string array containing all elements of the command line used

to execute the current application, including the application name. See

recipe 1-5 for details.



GetEnvironmentVariable



Returns a string containing the value of a specified environment

variable. See recipe 14-2 for details.



GetEnvironmentVariables



Returns an object implementing System.Collections.IDictionary,

which contains all environment variables and their values. See recipe 142 for details.



GetFolderPath



Returns a string containing the path to a special system folder specified

using the System.Environment.SpecialFolder enumeration. This

includes folders for the Internet cache, cookies, history, desktop, and

favorites; see the .NET Framework SDK documentation for a complete

list of values.



GetLogicalDrives



Returns a string array containing the names of all logical drives,

including network-mapped drives. Note that each drive has the following

syntax: :\.



685



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



The System.OperatingSystem object returned by OSVersion contains four properties:





The Platform property returns a value of the System.PlatformID enumeration

identifying the current operating system; valid values are Unix, Win32NT, Win32S,

Win32Windows, and WinCE.







The ServicePack property returns a string identifying the service pack level

installed on the computer. If no service packs are installed, or service packs are

not supported, an empty string is returned.







The Version property returns a System.Version object that identifies the specific

operating system version.







The VersionString property returns a concatenated string summary of the

Platform, ServicePack, and Version properties.



To determine the operating system on which you are running, you must use both the platform and

the version information, as detailed in Table 14-2.

Table 14-2. Determining the Current Operating System



PlatformID



Major Version



Minor Version



Operating System



Win32Windows



4



10



Windows 98



Win32Windows



4



90



Windows Me



Win32NT



4



0



Windows NT 4



Win32NT



5



0



Windows 2000



Win32NT



5



1



Windows XP



Win32NT



5



2



Windows Server 2003



Win32NT



6



0



Windows Vista, Windows Server 2008



Win32NT



6



1



Windows 7, Windows Server 2008 R2



The Code

The following example uses the Environment class to display information about the current environment

to the console:



686



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



using System;

namespace Apress.VisualCSharpRecipes.Chapter14

{

class Recipe14_01

{

public static void Main()

{

// Command line.

Console.WriteLine("Command line : " + Environment.CommandLine);

// OS and CLR version information.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("OS PlatformID : " +

Environment.OSVersion.Platform);

Console.WriteLine("OS Major Version : " +

Environment.OSVersion.Version.Major);

Console.WriteLine("OS Minor Version : " +

Environment.OSVersion.Version.Minor);

Console.WriteLine("CLR Version : " + Environment.Version);

// User, machine, and domain name information.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("User Name : " + Environment.UserName);

Console.WriteLine("Domain Name : " + Environment.UserDomainName);

Console.WriteLine("Machine name : " + Environment.MachineName);

// Other environment information.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Is interactive? : "

+ Environment.UserInteractive);

Console.WriteLine("Shutting down? : "

+ Environment.HasShutdownStarted);

Console.WriteLine("Ticks since startup : "

+ Environment.TickCount);

// Display the names of all logical drives.

Console.WriteLine(Environment.NewLine);

foreach (string s in Environment.GetLogicalDrives())

{

Console.WriteLine("Logical drive : " + s);

}

// Standard folder information.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Current folder : "

+ Environment.CurrentDirectory);

Console.WriteLine("System folder : "

+ Environment.SystemDirectory);



687



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



// Enumerate all special folders and display them.

Console.WriteLine(Environment.NewLine);

foreach (Environment.SpecialFolder s in

Enum.GetValues(typeof(Environment.SpecialFolder)))

{

Console.WriteLine("{0} folder : {1}",

s, Environment.GetFolderPath(s));

}

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}



14-2. Retrieve the Value of an Environment Variable

Problem

You need to retrieve the value of an environment variable for use in your application.



Solution

Use the GetEnvironmentVariable, GetEnvironmentVariables, and ExpandEnvironmentVariables methods

of the Environment class.



How It Works

The GetEnvironmentVariable method allows you to retrieve a string containing the value of a single

named environment variable, whereas the GetEnvironmentVariables method returns an object

implementing IDictionary that contains the names and values of all environment variables as strings.

The .NET Framework includes an overload of the GetEnvironmentVariables method that takes a

System.EnvironmentVariableTarget argument, allowing you to specify a subset of environment variables

to return based on the target of the variable: Machine, Process, or User.

The ExpandEnvironmentVariables method provides a simple mechanism for substituting the value of

an environment variable into a string by including the variable name enclosed in percent signs (%) within

the string.



688



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



The Code

Here is an example that demonstrates how to use all three methods:

using System;

using System.Collections;

namespace Apress.VisualCSharpRecipes.Chapter14

{

class Recipe14_02

{

public static void Main()

{

// Retrieve a named environment variable.

Console.WriteLine("Path = " +

Environment.GetEnvironmentVariable("Path"));

Console.WriteLine(Environment.NewLine);

// Substitute the value of named environment variables.

Console.WriteLine(Environment.ExpandEnvironmentVariables(

"The Path on %computername% is %Path%"));

Console.WriteLine(Environment.NewLine);

// Retrieve all environment variables targeted at the process and

// display the values of all that begin with the letter U.

IDictionary vars = Environment.GetEnvironmentVariables(

EnvironmentVariableTarget.Process);

foreach (string s in vars.Keys)

{

if (s.ToUpper().StartsWith("U"))

{

Console.WriteLine(s + " = " + vars[s]);

}

}

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}



689



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

14-1. Access Runtime Environment Information

Tải bản đầy đủ ngay(0 tr)

×