The My.Application
property basically allows managing the following environment information:
• Getting information on the ClickOnce deployment for the current application
• Retrieving environment variables
• Writing entries to the Windows Applications log
• Retrieving command line arguments
Next you see in detail how you can get/set such information using My.Application
.
It can be useful to get information on the state of the deployment if your application has been installed via the ClickOnce technology (discussed in detail in Chapter 55, “Deploying Applications with ClickOnce”). You might want to provide the ability of downloading files on-demand or to implement additional behaviors according to the updates status. You can use the IsNetworkDeployed
property to know if an application has been deployed to a network via ClickOnce and the My.Application.Deployment
property (which wraps System.Deployment.Application.ApplicationDeployment
) to make other decisions. The following code shows information on the deployment status only if the application has been deployed to a network:
The CurrentVersion
property is useful to understand the current deployment version, while ActivationUri
is the address where the application manifest is invoked from. You can also programmatically check for updates invoking specific methods, such as CheckForUpdate
, CheckForUpdateAsync
, and CheckForUpdateAsyncCancel
. The following is an example:
My.Application.Deployment.CheckForUpdate()
Luckily, My.Application.Deployment
members’ names are self-explanatory, and with the help of IntelliSense and a little bit of curiosity, you have in your hands all the power of such an object.
There are situations where you need to retrieve the content of the operating system’s environment variables. This can be accomplished by invoking the GetEnvironmentVariable
that receives the name of the variable as an argument. The following code shows how to retrieve the content of the PATH environment variable:
The .NET Framework provides several ways for interacting with the operating system logs, but the My
namespace offers an easy way to write information to the Windows application log. My.Application
offers a Log
property, of type Microsoft.VisualBasic.Logging.Log
, which exposes members for writing information. The following code snippet shows how you can write a message to the application log invoking the WriteEntry
method:
The first argument is the message, and the second one is a member of the TraceEventType
enumeration whose members are self-explanatory, thanks to IntelliSense, and allow specifying the level of your message. Alternatively, you can write the content of an entire exception invoking the WriteException
method:
You can also get control over the listeners and the file used by the .NET Framework by utilizing the TraceSource
and DefaultFileLogWriter
.
If you need to retrieve command-line arguments for your application, My.Application
offers a convenient way. To complete the following demonstration, go to the Debug tab of My Project and set whatever command-line arguments you like in the Command Line Arguments text box. My.Application
offers a CommandLineArgs
property, which is a ReadOnlyCollection(Of String)
that stores such arguments. Each item in the collection represents a command-line argument. The following code shows how you can iterate such collection and check for available command-line arguments:
18.224.109.21