C # Start ClickOnce

Source: Internet
Author: User
String menuShortcut = Path. combine (Environment. getFolderPath (Environment. specialFolder. programs), string. format (@ "{0} \ {1 }. appref-ms ", Application. companyName, Application. productName); Console. out. writeInfo (menuShortcut); string startupShortcut = Path. combine (Environment. getFolderPath (Environment. specialFolder. startup), Path. getFileName (menuShortcut); Console. out. writeInfo (startupShortcut); if (! File. exists (startupShortcut) {File. copy (menuShortcut, startupShortcut); Console. out. writeTip ("Boot OK. ");} else {File. delete (startupShortcut); Console. out. writeTip ("enable OK. ");}

 

Having issues getting your clickonce applications auto start when logging on to Vista? You have come to the right place! Below is an explanation of various approaches out there and the pros ans cons of each approach andLastly the best working approach!

Solution 1: Placing a shortcut cut in the startup folder.
One way to fix this problem was to programmatically create a shortcut cut in the users startup folder.
String publisherName = Application. CompanyName;
String productName = Application. ProductName;
String startupPath = Environment. GetFolderPath (Environment. SpecialFolder. Startup );
StartupPath = Path. Combine (startupPath, productName) + ". appref-ms ";
If (! File. Exists (startupPath ))
{
String allProgramsPath = Environment. GetFolderPath (Environment. SpecialFolder. Programs );
String shortcutPath = Path. Combine (allProgramsPath, publisherName );
ShortcutPath = Path. Combine (shortcutPath, productName) + ". appref-ms ";
File. Copy (shortcutPath, startupPath );
}
Whist this was a good approach but this still has issues.
Issue 1:
Dint work on all windows platforms. Some of my tests failed on one box with Vista Enterprise and strangely worked on another box with Vista Enterprise!

Issue 2:
Uninstalling the application dint remove the specified cut from the startup folder. Thus this wocould always pop up the installation of the app everytime you login even though you had uninstalled the application.

Solution 2: Creating a url between cut and placing in the startup folder.
On digging deep in google, I found an approach where you cocould create "url" shortcuts and place them in the startup folder. Whist this worked reliably on most windows platform, there was a catch!

Issue 1: This approach only works if you are connected to the internet at the time of logging in to windows.

Issue 2:
Uninstalling the application dint remove the specified cut from the startup folder. Thus this wocould always pop up the installation of the app everytime you login even though you had uninstalled the application.

Solution 3: Using the registry and turning off check for updates (Best approach ).
OK this is the best solution till now as per my experience. This works on all versions of vista where the above two approaches failed.

Just like solution1, you need to create a appref-ms shortcut. but this time it has to be in the current users registry as opposed to the startup folder. also this requires you to turn off check for updates through clickonce for it to work fully.

Creating the shortcut:
RegistryKey regkey = Registry. CurrentUser. OpenSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true );
String publisherName = Application. CompanyName;
String productName = Application. ProductName;
String allProgramsPath = Environment. GetFolderPath (Environment. SpecialFolder. Programs );
String shortcutPath = Path. Combine (allProgramsPath, publisherName );
ShortcutPath = Path. Combine (shortcutPath, productName) + ". appref-ms ";
Regkey. DeleteSubKey ("YourApplication", false); // delete old key if exists
Regkey. SetValue ("YourApplication", shortcutPath );

Once you have this in place, you need to ensure you uncheck the option "Application shocould check for updates" in the publish properties of your application. this is important. all this means is, you will have to implement code to check for updates when your application starts. this is easy and you can use Application. deployment namespace to achieve this. probably fire this "update check" code in your splash screen and force an application. restart if the application was updated. you can make this update process interactive by using Async CILS and refreshing you splashscreen with progress bar updates by subscribing to progress events.
Form Load:
ApplicationDeployment. CurrentDeployment. UpdateProgressChanged + = new DeploymentProgressChangedEventHandler (UpdateProgress );

Form Shown:
ApplicationDeployment. CurrentDeployment. CheckForUpdateAsync ();

Private void UpdateProgress (object sender, DeploymentProgressChangedEventArgs e)
{
Progressbar1.Value = e. ProgressPercentage;
Label1.Text = (e. BytesCompleted/1024). ToString () + "of" + (e. BytesTotal/1024). ToString () + "kb downloaded ";
}

After doing this you shoshould be all good to go!

I havent found any issues with this approach till now. Also this approach overcomes the issues with the abve two solutions.

Http://dipeshavlani.net/2009/05/12/clickonce-and-startup-on-windows-logon-vista/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.