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:

 
 Sub AboutBox_Load(sender As Object, e As EventArgs)   Me.companyNameTextBox.Text = Application.CompanyName   Me.productNameTextBox.Text = Application.ProductName   Me.productVersionTextBox.Text = Application.ProductVersion End Sub 

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 Applications")> ' 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.*")> 

Keep in mind that AssemblyVersionAttribute is calculated differently in Visual Basic .NET than in C#. For Visual Basic .NET projects, AssemblyVersionAttribute is incremented only once per VS.NET session, whereas in C# it is incremented once per compile. Visual Basic .NET programmers need to shut down VS.NET and restart it to get the attribute to increment.

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 that from the Environment object in the System namespace:

 
 NotInheritable Class Environment   ' Properties   Shared Property CommandLine() As String   Shared Property CurrentDirectory() As String   Shared Property ExitCode() As Integer   Property HasShutdownStarted() As Boolean   Shared Property MachineName() As String   Shared Property NewLine() As String   Shared Property OSVersion() As OperatingSystem   Shared Property StackTrace() As String   Shared Property SystemDirectory() As String   Shared Property TickCount() As Integer   Shared Property UserDomainName() As String   Shared Property UserInteractive() As Boolean   Shared Property UserName() As String   Shared Property Version() As Version   Shared Property WorkingSet() As Long   ' Methods   Shared Sub Exit(exitCode As Integer)   Shared Function ExpandEnvironmentVariables(name As String) _     As String   Shared Function GetCommandLineArgs() As String()   Shared Function GetEnvironmentVariable(variable As String) _     As String   Shared Function GetEnvironmentVariables() As IDictionary   Shared Function GetFolderPath(folder As SpecialFolder) As String   Shared Function GetLogicalDrives() As String() End Class 
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:

 
 <STAThread()> _ Shared Sub Main(args() As String)   Dim flag As Boolean = False   Dim name As String = ""   Dim number As Integer = 0   ' *Very* simple command line parsing   Dim i As Integer   For i = 0 to args.Length  1       Select Case args(i)           Case "/flag"               flag = True           Case "/name"               Name = args(i + 1)               i = i + 1           Case "/number"               number = Integer.Parse(args(i+1))               i = i + 1           Case Else               MessageBox.Show("invalid args!")               Exit Sub       End Select   Next   MessageBox.Show(flag.ToString(), "flag")   MessageBox.Show(name, "name")   MessageBox.Show(number.ToString(), "number")   ... End Sub 


Windows Forms Programming in Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

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