Skip to content

Commit

Permalink
Added autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
rahil627 committed Jan 11, 2012
1 parent af593cc commit 471f875
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 1 deletion.
44 changes: 44 additions & 0 deletions ClickOnce/AppShortcut.cs
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);
}
}
}
}
145 changes: 145 additions & 0 deletions ClickOnce/ClickOnceHelper.cs
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);
}
}
}
39 changes: 39 additions & 0 deletions ClickOnce/UrlShortcut.cs
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();
}
}
}
}
3 changes: 3 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ static class Program
[STAThread]
static void Main(string[] args)
{
//add application shortcut to startup folder
ClickOnce.AppShortcut.AutoStart(true);

//start the GUI
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Expand Down
6 changes: 5 additions & 1 deletion Trollkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<PublisherName>Babycastles</PublisherName>
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
<WebPage>publish.htm</WebPage>
<ApplicationRevision>12</ApplicationRevision>
<ApplicationRevision>14</ApplicationRevision>
<ApplicationVersion>0.1.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down Expand Up @@ -101,6 +101,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Forms.DataVisualization" />
Expand All @@ -113,6 +114,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ClickOnce\AppShortcut.cs" />
<Compile Include="ClickOnce\ClickOnceHelper.cs" />
<Compile Include="ClickOnce\UrlShortcut.cs" />
<Compile Include="Test\FullScreen.cs" />
<Compile Include="GlobalMouseKeyboard.cs" />
<Compile Include="Old\Keyboard.cs" />
Expand Down

0 comments on commit 471f875

Please sign in to comment.