Chapter 17: Windows Integration


The intention is for the Microsoft .NET Framework to run on a wide variety of operating systems to improve code mobility and simplify cross-platform integration. At the time this book was written, versions of the .NET Framework were available for various operating systems, including Microsoft Windows, FreeBSD, Linux, and Mac OS X. However, many of these implementations are still incomplete or yet to be widely adopted. Microsoft Windows is currently the operating system on which the .NET Framework is most commonly installed. The recipes in this chapter focus on performing the following tasks that are specific to the Windows operating system:

  • Retrieving run-time environment information (recipes 17.1 and 17.2)

  • Writing to the Windows event log (recipe 17.3)

  • Accessing the Windows registry (recipe 17.4)

  • Creating and installing Windows services (recipes 17.5 and 17.6)

  • Creating a shortcut on the Windows Start menu or desktop (recipe 17.7)

    Note  

    The majority of functionality discussed in this chapter is protected by code access security permissions enforced by the common language runtime (CLR). See Chapter 13 for information about code access security, and see the .NET Framework SDK documentation for the specific permissions required to execute each member.

17.1 Access Run-Time Environment Information

Problem

You need to access information about the run-time environment in which your application is running.

Solution

Use the members of the System.Environment class.

Discussion

The Environment class provides a set of static members that you can use to obtain (and in some cases modify) information about the environment in which an application is running. Table 17.1 describes some of the most commonly used Environment members.

Table 17.1: Commonly Used Members of the Environment Class

Member

Description

Property

 

CommandLine

Gets a string containing the command line used to execute the current application, including the application name ; see recipe 1.5 for details.

CurrentDirectory

Gets and sets a string containing the current application directory. Initially, this property will contain the name of the directory in which the application was started.

HasShutdownStarted

Gets a bool that indicates whether the CLR has started to shutdown or the current application domain has started unloading.

MachineName

Gets a string containing the name of the machine.

OSVersion

Gets a System.OperatingSystem object that contains information about the platform and version of the underlying operating system. See the paragraph following this table for more details.

SystemDirectory

Gets a string containing the fully qualified path of the system directory.

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 machine is stand-alone.

UserInteractive

Gets a bool indicating whether the application is running in user interactive mode. 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.

Version

Gets a System.Version object that contains information about the version of the CLR.

Method

 

ExpandEnvironmentVariables

Replaces the names of environment variables in a string with the value of the variable; see recipe 17.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 17.2 for details.

GetEnvironmentVariables

Returns a System.Collections.IDictionary containing all environment variables and their values; see recipe 17.2 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 .

The OperatingSystem object returned by OSVersion contains two properties: Platform and Version . The Platform property returns a value of the System.PlatformID enumeration identifying the current operating system; valid values are Win32NT , Win32S , Win32Windows , and WinCE . The Version property returns a System.Version object that identifies the specific operating system version. To determine the operating system on which you are running, you must use both the platform and the version information as detailed in Table 17-2.

Table 17.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

Windows NT 4

Win32NT

5

Windows 2000

Win32NT

5

1

Windows XP

Win32NT

5

2

Windows Server 2003

The AccessEnvironmentExample class uses the Environment class to display information about the current environment to the console.

 using System; public class AccessEnvironmentExample {     public static void Main() {         // Command line.         Console.WriteLine("Command line : " + Environment.CommandLine);         // OS and CLR version information.         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("User Name : " + Environment.UserName);         Console.WriteLine("Domain Name : " + Environment.UserDomainName);         Console.WriteLine("Machine name : " + Environment.MachineName);         // Other environment information.         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.         foreach (string s in Environment.GetLogicalDrives()) {             Console.WriteLine("Logical drive : " + s);         }         // Standard folder information.         Console.WriteLine("Current folder : "              + Environment.CurrentDirectory);         Console.WriteLine("System folder : "              + Environment.SystemDirectory);         // Enumerate all special folders and display them.         foreach (Environment.SpecialFolder s in              Enum.GetValues(typeof(Environment.SpecialFolder))) {             Console.WriteLine("{0} folder : {1}",                  s, Environment.GetFolderPath(s));         }         // Wait to continue.         Console.WriteLine("Main method complete. Press Enter.");         Console.ReadLine();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net