Appendix R : The My Namespace


The My namespace provides shortcuts to make performing common tasks easier. The following sections describe the major items within the My namespace and describe the tools that they make available.

My.Application

My.Application provides information about the current application. It includes properties that tell you the program’s current directory, culture, Log object, and splash screen. It also includes information about the application’s assembly, including the program’s version numbering.

The following table describes the most useful My.Application properties, methods, and events.

Open table as spreadsheet

Item

Purpose

ApplicationContext

Returns an ApplicationContext object for the currently executing thread. It provides a reference to the thread’s form. Its ExitThread method terminates the thread and its ThreadExit event fires when the thread is exiting.

ChangeCurrentCulture

Changes the thread’s culture used for string manipulation and formatting.

ChangeCurrentUICulture

Changes the thread’s culture used for retrieving resources.

CommandLineArgs

Returns a collection containing the command-line argument strings used when the application was started. The first entry (with index 0) is the fully qualified name of the executable application.

CurrentCulture

Returns a CultureInfo object that represents the settings used for culture-specific string manipulation and formatting. This includes calendar information, date and time specifications, the culture’s name, keyboard layout, number formats for general numbers (for example, the thousands separator character and decimal character), currency, and percentages.

CurrentUICulture

Returns a CultureInfo object that represents the culture-specific settings used by the thread to retrieve resources. It determines the culture used by the Resource Manager and My.Resources.

Deployment

Returns the application’s current ApplicationDeployment object used for ClickOnce deployment. Normally, you don’t need to manage deployment yourself, but this object lets you check for updates, start an update synchronously or asynchronously, download files, and restart the updated application.

DoEvents

Makes the application process all of the Windows messages currently waiting in the message queue. Doing this allows controls to process messages and update their appearances while the program is performing a long calculation. Often, you can avoid using DoEvents by performing long calculations on a separate thread, so the user interface can continue running normally.

GetEnvironmentVariable

Returns the value of the specified environment variable. For example, the following code displays the value of the PATH environment variable.

  MessageBox.Show(My.Application .GetEnvironmentVariable("PATH")) 

This method raises an exception if the named environment variable doesn’t exist. The method Environment .GetEnvironment?Variable performs the same function, except that it returns Nothing if the variable doesn’t exist.

Info

Returns an AssemblyInfo object that provides information about the assembly such as assembly name, company name, copyright, trademark, and version.

IsNetworkDeployed

Returns True if the application was deployed over the network. You should check this property and only try to use the My.Application.Deployment object if it returns True.

Log

An object of the class MyLog. You can use this object’s WriteEntry and WriteException methods to log messages and exceptions.

MainForm

Gets or sets the application’s main form.

NetworkAvailabilityChanged

The application raises this event when the network’s availability changes.

OpenForms

Returns a collection containing references to all of the application’s open forms.

Shutdown

The application raises this event when it is shutting down. This event occurs after all forms’ FormClosing and FormClosed event handlers have finished.

SplashScreen

Gets or sets the application’s splash screen.

Startup

The application raises this event when it is starting up.

StartupNextInstance

The application raises this event when the user tries to start a second instance of a single-instance application.

UICulture

Gets the thread’s culture used for retrieving resources.

UnhandledException

The application raises this event if it encounters an unhandled exception.

The following table lists the Info object’s properties.

Open table as spreadsheet

Property

Purpose

AssemblyName

Gets the assembly’s name

CompanyName

Gets the assembly’s company name

Copyright

Gets the assembly’s copyright information

Description

Gets the assembly’s description

DirectoryPath

Gets the directory where the assembly is stored

LoadedAssemblies

Returns a collection of Assembly objects for the application’s currently loaded assemblies

ProductName

Gets the assembly’s product name

StackTrace

Gets a stack trace

Title

Gets the assembly’s title

Trademark

Gets the assembly’s trademark information

Version

Gets the assembly’s version number

WorkingSet

Gets the number of bytes mapped to the process context

To set most of the Info values at design time, open Solution Explorer, double-click the My Project entry, select the Application tab as shown in Figure R-1, and click the Assembly Information button.

image from book
Figure R-1: Click the Assembly Information button to view and set assembly information.

Enter the assembly information in the dialog shown in Figure R-2 and click OK.

image from book
Figure R-2: Use this dialog box to enter information that the program can later retrieve using My.Application.AssemblyInfo.

To place code in the My.Application object’s NetworkAvailabilityChanged, Shutdown, Startup, StartupNextInstance, or UnhandledException event handlers, open Solution Explorer, double-click the My Project entry, select the Application tab, and click the View Application Events button at the bottom of Figure R-1.

Alternatively, you can open Solution Explorer, click the Show All Files button, expand the My Project entry, and open the file ApplicationEvents.vb.

To make the application a single-instance application, open the property page shown in Figure R-1 and select the Make single instance application check box in the middle of the page.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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