Redigera

Dela via


Access ClickOnce deployment properties for .NET on Windows

Starting in .NET 7 and Visual Studio 2022 version 17.4, you can access ClickOnce deployment properties by using an environment variable.

The application launcher shares ClickOnce application deployment properties with the application being launched (.NET only). Properties are shared with the application by using environment variables.

The variable names closely match the properties in the .NET Framework ApplicationDeployment class. The new variable names include a ClickOnce_ prefix:

In addition to these changes, a new property is available that returns the application launcher version:

  • ClickOnce_LauncherVersion

A .NET application can use these properties directly or indirectly.

Note

Using this method, you can access application deployment properties, but .NET 7 does not support the equivalent of ApplicationDeployment methods.

Access properties

The following code example shows how to access two properties directly, ClickOnce_IsNetworkDeployed and ClickOnce_ActivationUri.

NameValueCollection nameValueTable = new NameValueCollection();
if (Environment.GetEnvironmentVariable("ClickOnce_IsNetworkDeployed")?.ToLower() == "true")
{
    string value = Environment.GetEnvironmentVariable("ClickOnce_ActivationUri");
    Uri activationUri = string.IsNullOrEmpty(value) ? null : new Uri(value);
    if (activationUri != null)
    {
        nameValueTable = HttpUtility.ParseQueryString(activationUri.Query);
        Console.WriteLine("Query string: " + activationUri.Query);
        Console.ReadKey();
    }
}

Indirect usage of these properties requires the implementation of a new ApplicationDeployment class at the application level. This class abstracts the reading of environment variables and provides a similar experience to the old .NET Framework class.

For a sample implementation of this class, see ApplicationDeployment.cs.

The following code snippet shows how to use this class:

NameValueCollection nameValueTable = new NameValueCollection();
if (ApplicationDeployment.IsNetworkDeployed)
{
    ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
    if (ad.ActivationUri != null)
    {
        nameValueTable = HttpUtility.ParseQueryString(ad.ActivationUri.Query);
    }
}

ActivationUri and URL parameters

Starting in .NET 7, dotnet-mage supports a new switch, -TrustURLParameters or -tu. This switch allows you to set the required deployment attribute by using the dotnet-mage tool. This change is an improvement over the old Mage tool, which didn't support this functionality and also required manual modification of the application manifest to add the trustURLParameters attribute, <deployment install="true" trustURLParameters="true">.

You need to set trustURLParameters to true to allow the application to access the ActivationUri and URL parameters.

ClickOnce for .NET on Windows