-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace ClickOnce | ||
{ | ||
/// <summary> | ||
/// Adds the ClickOnce application shortcut to the startup folder. | ||
/// It also updates the shortcut if the ClickOnce application is updated. | ||
/// </summary> | ||
/// <remarks>http://johnnycoder.com/blog/2009/02/24/clickonce-run-at-startup/</remarks> | ||
internal class AppShortcut | ||
{ | ||
public static void AutoStart(bool enable) | ||
{ | ||
if (!ClickOnceHelper.IsApplicationNetworkDeployed) | ||
throw new Exception("This application was not installed using ClickOnce."); | ||
|
||
String startupShortcut = ClickOnceHelper.GetStartupShortcut(ClickOnceHelper.AssemblyProductName, | ||
ClickOnceHelper.ShortcutType.Application); | ||
|
||
// Always remove the startup shortcut if it exists. | ||
// This will handling disabling the run at startup functionality | ||
// or ensure the most recent shortcut is copied into the Startup folder if we're enabling. | ||
if (File.Exists(startupShortcut)) | ||
{ | ||
File.Delete(startupShortcut); | ||
} | ||
|
||
if (!enable) | ||
{ | ||
return; | ||
} | ||
|
||
String programShortcut = ClickOnceHelper.GetProgramShortcut(ClickOnceHelper.AssemblyCompanyName, | ||
ClickOnceHelper.AssemblyProductName); | ||
|
||
if (File.Exists(programShortcut)) | ||
{ | ||
// Enable run at startup by copying the progam shortcut into the startup folder. | ||
File.Copy(programShortcut, startupShortcut); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using System; | ||
using System.Deployment.Application; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace ClickOnce | ||
{ | ||
public class ClickOnceHelper | ||
{ | ||
public enum ShortcutType | ||
{ | ||
Application, | ||
Url | ||
} | ||
|
||
public static bool IsApplicationNetworkDeployed | ||
{ | ||
get | ||
{ | ||
if (ApplicationDeployment.IsNetworkDeployed) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
public static Uri ActivationUri | ||
{ | ||
get | ||
{ | ||
if (ApplicationDeployment.IsNetworkDeployed) | ||
{ | ||
return ApplicationDeployment.CurrentDeployment.ActivationUri; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public static String StartUpUri | ||
{ | ||
get | ||
{ | ||
string startUpURL = String.Empty; | ||
|
||
if (ApplicationDeployment.IsNetworkDeployed && | ||
ApplicationDeployment.CurrentDeployment.UpdateLocation != null) | ||
{ | ||
startUpURL = ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri; | ||
|
||
if (!string.IsNullOrEmpty(ApplicationDeployment.CurrentDeployment.UpdateLocation.Query)) | ||
{ | ||
startUpURL = startUpURL.Replace(ApplicationDeployment.CurrentDeployment.UpdateLocation.Query, | ||
String.Empty); | ||
} | ||
} | ||
|
||
return Uri.UnescapeDataString(startUpURL); | ||
} | ||
} | ||
|
||
public static string IconLocation | ||
{ | ||
get | ||
{ | ||
Assembly assembly = Assembly.GetExecutingAssembly(); | ||
String iconLocation = assembly.Location; | ||
|
||
if (!string.IsNullOrEmpty(iconLocation)) | ||
return iconLocation.Replace('\\', '/'); | ||
|
||
return string.Empty; | ||
} | ||
} | ||
|
||
public static string AssemblyProductName | ||
{ | ||
get | ||
{ | ||
Assembly assembly = Assembly.GetExecutingAssembly(); | ||
|
||
if (!Attribute.IsDefined(assembly, typeof (AssemblyProductAttribute))) | ||
return string.Empty; | ||
|
||
var product = | ||
(AssemblyProductAttribute) Attribute.GetCustomAttribute(assembly, typeof (AssemblyProductAttribute)); | ||
|
||
return product.Product; | ||
} | ||
} | ||
|
||
public static string AssemblyCompanyName | ||
{ | ||
get | ||
{ | ||
Assembly assembly = Assembly.GetExecutingAssembly(); | ||
|
||
if (!Attribute.IsDefined(assembly, typeof (AssemblyCompanyAttribute))) | ||
return string.Empty; | ||
|
||
var company = | ||
(AssemblyCompanyAttribute) Attribute.GetCustomAttribute(assembly, typeof (AssemblyCompanyAttribute)); | ||
|
||
return company.Company; | ||
} | ||
} | ||
|
||
public static string GetApplicationExecutable() | ||
{ | ||
return Assembly.GetExecutingAssembly().GetName().CodeBase; | ||
} | ||
|
||
public static string GetProgramShortcut(String publisher, String product) | ||
{ | ||
string allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs); | ||
string shortcutPath = Path.Combine(allProgramsPath, publisher); | ||
shortcutPath = Path.Combine(shortcutPath, product) + ".appref-ms"; | ||
return shortcutPath; | ||
} | ||
|
||
public static string GetStartupShortcut(string product, ShortcutType shortcutType) | ||
{ | ||
string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); | ||
startupPath = Path.Combine(startupPath, product); | ||
|
||
switch (shortcutType) | ||
{ | ||
case ShortcutType.Application: | ||
startupPath += ".appref-ms"; | ||
break; | ||
case ShortcutType.Url: | ||
startupPath += ".url"; | ||
break; | ||
} | ||
|
||
return startupPath; | ||
} | ||
|
||
public static Boolean DoesStartupShortcutExist(string product, ShortcutType shortcutType) | ||
{ | ||
String file = GetStartupShortcut(product, shortcutType); | ||
return File.Exists(file); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace ClickOnce | ||
{ | ||
internal class UrlShortcut | ||
{ | ||
public static void AutoStart(bool enable) | ||
{ | ||
if (!ClickOnceHelper.IsApplicationNetworkDeployed) | ||
throw new Exception("This application was not installed using ClickOnce."); | ||
|
||
String startupShortcut = ClickOnceHelper.GetStartupShortcut(ClickOnceHelper.AssemblyProductName, | ||
ClickOnceHelper.ShortcutType.Url); | ||
|
||
// Always remove the startup shortcut if it exists. | ||
// This will handling disabling the run at startup functionality | ||
// or ensure the most recent shortcut is copied into the Startup folder if we're enabling. | ||
if (File.Exists(startupShortcut)) | ||
{ | ||
File.Delete(startupShortcut); | ||
} | ||
|
||
if (!enable) | ||
{ | ||
return; | ||
} | ||
|
||
using (var writer = new StreamWriter(startupShortcut)) | ||
{ | ||
writer.WriteLine("[InternetShortcut]"); | ||
writer.WriteLine("URL=" + ClickOnceHelper.StartUpUri); | ||
writer.WriteLine("IconIndex=0"); | ||
writer.WriteLine("IconFile=" + ClickOnceHelper.IconLocation); | ||
writer.Flush(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters