1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
using System.Deployment.Application;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
namespace MediaBrowser.ClickOnce
{
/// <summary>
/// Class ClickOnceHelper
/// </summary>
public class ClickOnceHelper
{
/// <summary>
/// The uninstall string
/// </summary>
private const string UninstallString = "UninstallString";
/// <summary>
/// The display name key
/// </summary>
private const string DisplayNameKey = "DisplayName";
/// <summary>
/// The uninstall string file
/// </summary>
private const string UninstallStringFile = "UninstallString.bat";
/// <summary>
/// The appref extension
/// </summary>
private const string ApprefExtension = ".appref-ms";
/// <summary>
/// The uninstall registry key
/// </summary>
private readonly RegistryKey UninstallRegistryKey;
/// <summary>
/// Gets the location.
/// </summary>
/// <value>The location.</value>
private static string Location
{
get { return Assembly.GetExecutingAssembly().Location; }
}
/// <summary>
/// Gets a value indicating whether this instance is network deployed.
/// </summary>
/// <value><c>true</c> if this instance is network deployed; otherwise, <c>false</c>.</value>
public static bool IsNetworkDeployed
{
get { return ApplicationDeployment.IsNetworkDeployed; }
}
/// <summary>
/// Gets the name of the publisher.
/// </summary>
/// <value>The name of the publisher.</value>
public string PublisherName { get; private set; }
/// <summary>
/// Gets the name of the product.
/// </summary>
/// <value>The name of the product.</value>
public string ProductName { get; private set; }
/// <summary>
/// Gets the uninstall file.
/// </summary>
/// <value>The uninstall file.</value>
public string UninstallFile { get; private set; }
/// <summary>
/// Gets the name of the suite.
/// </summary>
/// <value>The name of the suite.</value>
public string SuiteName { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ClickOnceHelper" /> class.
/// </summary>
/// <param name="publisherName">Name of the publisher.</param>
/// <param name="productName">Name of the product.</param>
/// <param name="suiteName">Name of the suite.</param>
public ClickOnceHelper(string publisherName, string productName, string suiteName)
{
PublisherName = publisherName;
ProductName = productName;
SuiteName = suiteName;
var publisherFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), PublisherName);
if (!Directory.Exists(publisherFolder))
{
Directory.CreateDirectory(publisherFolder);
}
UninstallFile = Path.Combine(publisherFolder, UninstallStringFile);
UninstallRegistryKey = GetUninstallRegistryKeyByProductName(ProductName);
}
/// <summary>
/// Gets the shortcut path.
/// </summary>
/// <returns>System.String.</returns>
private string GetShortcutPath()
{
var allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
var shortcutPath = Path.Combine(allProgramsPath, PublisherName);
if (!string.IsNullOrEmpty(SuiteName))
{
shortcutPath = Path.Combine(shortcutPath, SuiteName);
}
return Path.Combine(shortcutPath, ProductName) + ApprefExtension;
}
/// <summary>
/// Gets the startup shortcut path.
/// </summary>
/// <returns>System.String.</returns>
private string GetStartupShortcutPath()
{
var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
return Path.Combine(startupPath, ProductName) + ApprefExtension;
}
/// <summary>
/// Adds the shortcut to startup.
/// </summary>
public void AddShortcutToStartup()
{
var startupPath = GetStartupShortcutPath();
if (!File.Exists(startupPath))
{
File.Copy(GetShortcutPath(), startupPath);
}
}
/// <summary>
/// Removes the shortcut from startup.
/// </summary>
public void RemoveShortcutFromStartup()
{
var startupPath = GetStartupShortcutPath();
if (File.Exists(startupPath))
{
File.Delete(startupPath);
}
}
/// <summary>
/// Updates the uninstall parameters.
/// </summary>
/// <param name="uninstallerFileName">Name of the uninstaller file.</param>
public void UpdateUninstallParameters(string uninstallerFileName)
{
if (UninstallRegistryKey != null)
{
UpdateUninstallString(uninstallerFileName);
}
}
/// <summary>
/// Gets the name of the uninstall registry key by product.
/// </summary>
/// <param name="productName">Name of the product.</param>
/// <returns>RegistryKey.</returns>
private RegistryKey GetUninstallRegistryKeyByProductName(string productName)
{
var subKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
if (subKey == null)
{
return null;
}
return subKey.GetSubKeyNames()
.Select(name => subKey.OpenSubKey(name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.QueryValues | RegistryRights.ReadKey | RegistryRights.SetValue))
.Where(application => application != null)
.FirstOrDefault(application => application.GetValueNames().Where(appKey => appKey.Equals(DisplayNameKey)).Any(appKey => application.GetValue(appKey).Equals(productName)));
}
/// <summary>
/// Updates the uninstall string.
/// </summary>
/// <param name="uninstallerFileName">Name of the uninstaller file.</param>
private void UpdateUninstallString(string uninstallerFileName)
{
var uninstallString = (string)UninstallRegistryKey.GetValue(UninstallString);
if (!string.IsNullOrEmpty(UninstallFile) && uninstallString.StartsWith("rundll32.exe", StringComparison.OrdinalIgnoreCase))
{
File.WriteAllText(UninstallFile, uninstallString);
}
var str = String.Format("\"{0}\" uninstall", Path.Combine(Path.GetDirectoryName(Location), uninstallerFileName));
UninstallRegistryKey.SetValue(UninstallString, str);
}
/// <summary>
/// Uninstalls this instance.
/// </summary>
public void Uninstall()
{
RemoveShortcutFromStartup();
var uninstallString = File.ReadAllText(UninstallFile);
new Process
{
StartInfo =
{
Arguments = uninstallString.Substring(uninstallString.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1),
FileName = uninstallString.Substring(0, uninstallString.IndexOf(" ", StringComparison.OrdinalIgnoreCase)),
UseShellExecute = false
}
}.Start();
}
/// <summary>
/// Configures the click once startup.
/// </summary>
/// <param name="publisherName">Name of the publisher.</param>
/// <param name="productName">Name of the product.</param>
/// <param name="suiteName">Name of the suite.</param>
/// <param name="runAtStartup">if set to <c>true</c> [run at startup].</param>
/// <param name="uninstallerFilename">The uninstaller filename.</param>
public static void ConfigureClickOnceStartupIfInstalled(string publisherName, string productName, string suiteName, bool runAtStartup, string uninstallerFilename)
{
if (!ApplicationDeployment.IsNetworkDeployed)
{
return;
}
var clickOnceHelper = new ClickOnceHelper(publisherName, productName, suiteName);
if (runAtStartup)
{
clickOnceHelper.UpdateUninstallParameters(uninstallerFilename);
clickOnceHelper.AddShortcutToStartup();
}
else
{
clickOnceHelper.RemoveShortcutFromStartup();
}
}
}
}
|