|
| 1 | +--- |
| 2 | +title: Access ClickOnce deployment properties for .NET |
| 3 | +description: "Learn how to access ClickOnce deployment properties for .NET Core 3.1, .NET 5 and later." |
| 4 | +ms.date: 11/21/2022 |
| 5 | +ms.topic: how-to |
| 6 | +helpviewer_keywords: |
| 7 | + - "deployment properties, ClickOnce for .NET 5+" |
| 8 | +author: mikejo5000 |
| 9 | +ms.author: mikejo |
| 10 | +manager: jmartens |
| 11 | +ms.technology: vs-ide-deployment |
| 12 | +monikerRange: '>= vs-2022' |
| 13 | +ms.workload: |
| 14 | + - "multiple" |
| 15 | +--- |
| 16 | +# Access ClickOnce deployment properties for .NET on Windows |
| 17 | + |
| 18 | + [!INCLUDE [Visual Studio](~/includes/applies-to-version/vs-windows-only.md)] |
| 19 | + |
| 20 | +Starting in .NET 7 and Visual Studio 2022 version 17.4, you can access ClickOnce deployment properties using an environment variable. |
| 21 | + |
| 22 | +The application launcher shares ClickOnce application deployment properties with the application being launched (.NET only). Properties are shared with the application using environment variables. |
| 23 | + |
| 24 | +The variable names closely match the properties in the .NET Framework <xref:System.Deployment.Application.ApplicationDeployment> class. The new variable names include a ClickOnce_ prefix: |
| 25 | + |
| 26 | +- [ClickOnce_IsNetworkDeployed](/dotnet/api/system.deployment.application.applicationdeployment.isnetworkdeployed) |
| 27 | +- [ClickOnce_ActivationUri](/dotnet/api/system.deployment.application.applicationdeployment.activationuri) |
| 28 | +- [ClickOnce_CurrentVersion](/dotnet/api/system.deployment.application.applicationdeployment.currentversion) |
| 29 | +- [ClickOnce_DataDirectory](/dotnet/api/system.deployment.application.applicationdeployment.datadirectory) |
| 30 | +- [ClickOnce_IsFirstRun](/dotnet/api/system.deployment.application.applicationdeployment.isfirstrun) |
| 31 | +- [ClickOnce_TimeOfLastUpdateCheck](/dotnet/api/system.deployment.application.applicationdeployment.timeoflastupdatecheck) |
| 32 | +- [ClickOnce_UpdatedApplicationFullName](/dotnet/api/system.deployment.application.applicationdeployment.updatedapplicationfullname) |
| 33 | +- [ClickOnce_UpdatedVersion](/dotnet/api/system.deployment.application.applicationdeployment.updatedversion) |
| 34 | +- [ClickOnce_UpdateLocation](/dotnet/api/system.deployment.application.applicationdeployment.updatelocation) |
| 35 | + |
| 36 | +In addition to these, a new property is available that returns the application launcher version: |
| 37 | + |
| 38 | +- ClickOnce_LauncherVersion |
| 39 | + |
| 40 | +A .NET application can use these properties directly or indirectly. |
| 41 | + |
| 42 | +## Access properties |
| 43 | + |
| 44 | +The following code example shows how to access two properties directly, `ClickOnce_IsNetworkDeployed` and `ClickOnce_ActivationUri`. |
| 45 | + |
| 46 | +```csharp |
| 47 | +NameValueCollection nameValueTable = new NameValueCollection(); |
| 48 | +if (Environment.GetEnvironmentVariable("ClickOnce_IsNetworkDeployed")?.ToLower() == "true") |
| 49 | +{ |
| 50 | + string value = Environment.GetEnvironmentVariable("ClickOnce_ActivationUri"); |
| 51 | + Uri activationUri = string.IsNullOrEmpty(value) ? null : new Uri(value); |
| 52 | + if (activationUri != null) |
| 53 | + { |
| 54 | + nameValueTable = HttpUtility.ParseQueryString(activationUri.Query); |
| 55 | + Console.WriteLine("Query string: " + activationUri.Query); |
| 56 | + Console.ReadKey(); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +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. |
| 62 | + |
| 63 | +For a sample implementation of this class, see [ApplicationDeployment.cs](https://github.com/dotnet/deployment-tools/blob/main/Documentation/dotnet-mage/ApplicationDeployment.cs). |
| 64 | + |
| 65 | +With the addition of this class, you can use it as follows: |
| 66 | + |
| 67 | +```csharp |
| 68 | +NameValueCollection nameValueTable = new NameValueCollection(); |
| 69 | +if (ApplicationDeployment.IsNetworkDeployed) |
| 70 | +{ |
| 71 | + ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; |
| 72 | + if (ad.ActivationUri != null) |
| 73 | + { |
| 74 | + nameValueTable = HttpUtility.ParseQueryString(ad.ActivationUri.Query); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## ActivationUri and URL parameters |
| 80 | + |
| 81 | +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"\> |
| 82 | + |
| 83 | +You need to set trustURLParameters to true to allow the application to access ActivationUri and the URL parameters. |
| 84 | + |
| 85 | +## See also |
| 86 | + |
| 87 | +[ClickOnce for .NET on Windows](../deployment/clickonce-deployment-dotnet.md) |
0 commit comments