Skip to content

Commit bc0ccae

Browse files
Merge pull request #10638 from Mikejo5000/mikejo-br16
Access application deployment properties in .NET
2 parents f4690dd + e159cc2 commit bc0ccae

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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)

docs/deployment/clickonce-deployment-dotnet.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: ClickOnce for .NET 5 and later on Windows
33
description: "Learn about differences between ClickOnce for .NET Core 3.1, .NET 5 and later versus ClickOnce for .NET Framework."
4-
ms.date: 09/14/2022
4+
ms.date: 11/22/2022
55
ms.topic: how-to
66
helpviewer_keywords:
77
- "deployment, ClickOnce for .NET 5+"
@@ -29,7 +29,11 @@ For building from the command line using MSBUILD, you need to specify the *.pubx
2929

3030
## ApplicationDeployment class
3131

32-
In .NET Core 3.1 and .NET 5 and later, you don't have programmatic access to the <xref:System.Deployment.Application.ApplicationDeployment> class or to other APIs in the <xref:System.Deployment.Application> namespace.
32+
In .NET Core 3.1, .NET 5, and .NET 6, you don't have programmatic access to the <xref:System.Deployment.Application.ApplicationDeployment> class or to other APIs in the <xref:System.Deployment.Application> namespace.
33+
34+
::: moniker range=">=vs-2022"
35+
Starting in .NET 7, you can access properties in the `ApplicationDeployment` class using environment variables. For more information, see [Access ClickOnce deployment properties in .NET](../deployment/access-clickonce-deployment-properties-dotnet.md).
36+
::: moniker-end
3337

3438
## Mage.exe
3539

docs/deployment/includes/dotnet-support-application-deployment-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ title: Visual Studio .NET ClickOnce API support
33
author: mikejo5000
44
description: Learn about .NET ClickOnce API support in ClickOnce
55
ms.author: mikejo
6-
ms.date: 09/12/2022
6+
ms.date: 11/22/2022
77
ms.technology: vs-ide-deployment
88
ms.topic: include
99
---
1010

1111
> [!NOTE]
12-
> The <xref:System.Deployment.Application.ApplicationDeployment> class and APIs in the <xref:System.Deployment.Application> namespace are not supported in .NET Core or .NET 5 and later.
12+
> The <xref:System.Deployment.Application.ApplicationDeployment> class and APIs in the <xref:System.Deployment.Application> namespace are not supported in .NET Core and .NET 5 and later versions. However, .NET 7 supports a new method of accessing application deployment properties. For more information, see [Access ClickOnce deployment properties in .NET](../../deployment/access-clickonce-deployment-properties-dotnet.md).

docs/deployment/toc.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@
5555
items:
5656
- name: Deploy to Azure using GitHub Actions
5757
href: azure-deployment-using-github-actions.md
58-
- name: ClickOnce security and deployment
58+
- name: ClickOnce
5959
items:
6060
- name: Overview of ClickOnce security and deployment
6161
href: clickonce-security-and-deployment.md
62-
- name: ClickOnce for .NET on Windows
63-
href: clickonce-deployment-dotnet.md
62+
- name: ClickOnce for .NET
63+
items:
64+
- name: ClickOnce for .NET on Windows
65+
href: clickonce-deployment-dotnet.md
66+
- name: Access ClickOnce deployment properties for .NET
67+
href: access-clickonce-deployment-properties-dotnet.md
6468
- name: Choose a ClickOnce deployment strategy
6569
href: choosing-a-clickonce-deployment-strategy.md
6670
- name: ClickOnce cache overview

0 commit comments

Comments
 (0)