Access ClickOnce deployment properties for .NET on Windows

Applies to: yesVisual Studio noVisual Studio for Mac noVisual Studio Code

Starting in .NET 7 and Visual Studio 2022 version 17.4, you can access ClickOnce deployment properties 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 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, a new property is available that returns the application launcher version:

  • ClickOnce_LauncherVersion

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

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, that abstracts the reading of environment variables and provides an experience that is very similar to old .NET Framework class.

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

With the addition of this class, you can use it as follows:

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

In .NET 7, dotnet-mage supports a new switch, -TrustURLParameters or -tu. This switch allows you to set the required deployment attribute using the dotnet-mage tool. This is an improvement over old Mage tool, which did not support this functionality and instead required you to manually modify the application manifest to add the trustURLParameters attribute, as follows: <deployment install="true" trustURLParameters="true">

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

See also

ClickOnce for .NET on Windows