Java programmers who develop applications for the Microsoft Windows platform will be pleasantly surprised by the integration features provided by the Microsoft .NET Framework. In this appendix, we’ll demonstrate the integration between .NET and some of the more commonly used Windows features, including the following:
The operating system runtime environment
System processes
The Windows registry
The event log
Windows Services
All of these features are supported directly by .NET classes without intermediate mechanisms such as Java Native Interface (JNI), COM, or native DLLs; however, the behavior of these features depends on the version of Windows being used and could affect the future portability of an application’s code.
The majority of the features described in this appendix require that the executing code or the current user have the necessary security permission to carry out the desired action. A full description of .NET Framework security is included in Chapter 17; consult the Windows documentation for details of platform security.
The System.Environment class provides a set of static utility members that give access to the runtime environment of the current process; we explore some of the more commonly used features in the following sections.
The Main method of an application can optionally take a String array argument; on execution, the runtime passes any command-line arguments as members of this array. Alternatively, the Environment.CommandLine property returns a String containing the full command line used to start the current process, whereas the Environment.GetCommandLineArgs method returns a String array containing the individual command-line components. Key benefits of these members include the following:
The program name used to launch the current application is included as the first component of the returned data; this isn’t available via the arguments to the Main method.
The command-line arguments are available irrespective of whether the Main method declares a String array argument.
The command-line information is easily accessible from any component of an application.
The Environment.GetEnvironmentVariable method returns a String containing the value of a specified environment variable, or null if the variable doesn’t exist. The GetEnvironmentVariables method returns an IDictionary instance containing all environment variables; the variable names are the dictionary keys.
The Environment.ExpandEnvironmentVariables method takes a single String argument; any variable names enclosed by the percent (%) character are expanded in the return value. For example, the following statement uses ExpandEnvironmentVariables to formulate a string containing the values of the OS and SystemRoot environment variables:
System.Console.WriteLine(Environment.ExpandEnvironmentVariables( "OS Version = %OS% and SystemRoot = %SystemRoot%"));
On a Windows 2000 machine, the output will be similar to the following:
OS Version = Windows_NT and SystemRoot = C:WINNT
Table A-1 summarizes four members of the Environment class that provide information about the configuration of drives and directories on the current machine.
Table A-1. Drive- and Directory-Related Members of the Environment Class
Description | |
---|---|
Properties | |
CurrentDirectory | Gets or sets the fully qualified path of the current application directory; initially this property will return the directory from which the application was started. |
SystemDirectory | Gets the fully qualified path of the system directory—for example, C:WINNTSystem32. |
Methods | |
GetFolderPath() | Returns the fully qualified path of a special system folder. The folder to return is identified using a value from the Environment.SpecialFolder enumeration. SpecialFolder includes values such as System, Templates, InternetCache, and Recent; consult the .NET documentation for full details. |
GetLogicalDrives() | Returns a String array containing the names of all logical drives configured on the computer. For example, on most computers with a hard drive, the return value would include the logical drive name C:. |
The Environment class provides properties that return information about the machine and the operating system; these are summarized in Table A-2.
Table A-2. Machine- and OS-Related Properties of the Environment Class
Property | Description |
---|---|
MachineName | Gets a String that is the name of the machine. |
OSVersion | Gets a System.OperatingSystem instance that encapsulates platform and version information about the underlying operating system. |
TickCount | Gets an int representing the number of milliseconds that have passed since the computer was started. |
UserName | Gets a String that is the name of the user that started the current process. |
Version | Gets a System.Version instance that contains version information about the currently running CLR. See the .NET documentation for details of System.Version. |
WorkingSet | Gets a long value indicating the amount of physical memory mapped to the current process. |