Environment


During its lifetime, an application runs in a certain environment. The environment is provided by a combination of compile-time and run-time settings supplied by .NET and Windows.

Compile-Time Settings

The Application class exposes several properties that provide the company name, product name , and product version of the currently running application:

 void AboutBox_Load(object sender, EventArgs e) {   this.companyNameTextBox.Text =  Application.CompanyName;  this.productNameTextBox.Text =  Application.ProductName;  this.productVersionTextBox.Text =  Application.ProductVersion;  } 

By default, these three values come from assemblywide AssemblyCompanyAttribute, AssemblyProductAttribute, and AssemblyVersionAttribute (provided in a wizard-generated file called AssemblyInfo.cs):

 [assembly: AssemblyCompanyAttribute("Sells Brothers, Inc.")] [assembly: AssemblyProductAttribute("Environment Test Application")] // Version info for an assembly consists of the following values: //  major.minor.build.revision // You can specify all the values, or you can default the Revision and // Build Numbers by using the '*' as shown below: [assembly: AssemblyVersionAttribute("1.0.*")] 

Not only will the values you put into these attributes be available using the Application properties, but they'll also be bundled into the Win32 version information for the assembly, as shown by the Version property page in Explorer in Figure 11.5.

Figure 11.5. Assembly Properties Shown in the Shell

The rest of the version information is set via other assembly-level attributes:

 [assembly: AssemblyTitle("Environment Test Title")] [assembly: AssemblyDescription("Environment Test Description")] [assembly: AssemblyCompany("Sells Brothers, Inc.")] [assembly: AssemblyProduct("Environment Test Application")] [assembly: AssemblyCopyright("Copyright (c) 2003, Chris Sells")] [assembly: AssemblyTrademark("Trademark nobody")] [assembly: AssemblyVersion("1.0.*")] 

Environment Settings

If you'd like to know where your application was started from in the file system or which folder it was run from, you can get that information from the ExecutablePath and StartupPath properties of the Application object. Table 11.1 shows examples of each.

If you want more environment settings, such as the environment variables or the command line string, you can get them from the Environment object in the System namespace:

 sealed class Environment {   // Properties  public static string CommandLine { get; }   public static string CurrentDirectory { get; set; }  public static int ExitCode { get; set; }   public bool HasShutdownStarted { get; }  public static string MachineName { get; }  public static string NewLine { get; }  public static OperatingSystem OSVersion { get; }  public static string StackTrace { get; }   public static string SystemDirectory { get; }   public static int TickCount { get; }   public static string UserDomainName { get; }   public static bool UserInteractive { get; }  public static string UserName { get; }  public static Version Version { get; }   public static long WorkingSet { get; }   // Methods   public static void Exit(int exitCode);   public static string ExpandEnvironmentVariables(string name);  public static string[] GetCommandLineArgs();   public static string GetEnvironmentVariable(string variable);  public static IDictionary GetEnvironmentVariables();  public static string GetFolderPath(SpecialFolder folder);  public static string[] GetLogicalDrives(); } 
Table 11.1. The Application ExecutablePath and StartupPath Properties

Application Class Static Property

Sample Property Value

ExecutablePath

D:\data\WinForms Book\src\ch11\Settings\bin\Debug\SettingsTest.exe

StartupPath

D:\data\WinForms Book\src\ch11\Settings\bin\Debug

As shown earlier, the command line arguments are also available as the array of strings argument passed to Main:

 [STAThreadAttribute] static void Main(  string[] args  ) {   bool flag = false;   string name = "";   int number = 0;  // *Very* simple command line parsing   for( int i = 0; i != args.Length; ++i ) {   switch( args[i] ) {  case "/flag": flag = true; break;       case "/name": name = args[++i]; break;       case "/number": number = int.Parse(args[++i]); break;       default: MessageBox.Show("invalid args!"); return;  }   }  MessageBox.Show(flag.ToString(), "flag");   MessageBox.Show(name, "name");   MessageBox.Show(number.ToString(), "number");   ... } 

If you want to see more robust command line parsing support, see the Genghis class library available at http://www.genghisgroup.com.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

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