Monday, June 29, 2009

Create Windows startup shortcut for ClickOnce application

ClickOnce does not provide an option for adding the application to start automatically when starting Windows. You can use the concepts in the following method to add or remove this option. You will need to make sure “Allow URL parameters to be passed to the application” is checked. This if found within Project Properties –> Publish tab –> Options –> Manifests.

using System;
using System.Deployment.Application; //Requires System.Deployment assembly
using System.IO;
using System.Text;
using System.Windows.Forms;
...
private void CreateWindowsStartup(bool create)
{
string startupFileName = Environment.GetFolderPath(System.Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".appref-ms";
string applicationShortcut = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
if (create)
{
if (!File.Exists(startupFileName))
{
using (FileStream fs = new FileStream(startupFileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Unicode))
{
sw.WriteLine(applicationShortcut);
}
}
}
}
else
{
if (File.Exists(startupFileName))
{
File.Delete(startupFileName);
}
}
}



 




4 comments:

  1. very nice information about Create Windows startup shortcut for ClickOnce application.really nice good tips.because That's impossible, simply because until you press the Start/Power Button there's no power on the keyboard to do anything.

    ReplyDelete
  2. ... and what's happening when we do uninstall the application ?

    I bet the created shortcut remains... hum.

    Any idea ?

    ReplyDelete
  3. Sorry for such a noob question,but where does these codes go to?

    ReplyDelete
  4. where do you put this code?

    ReplyDelete