C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Run your application at start up


Posted Date: 22 May 2008    Resource Type: Code Snippets    Category: Registry Operations
Author: Payal JainMember Level: Gold    
Rating: Points: 10



introduction


this code will help you to add your application to the windows startup.

namespace


namespace used for this is Microsoft.Win32 which will be imported to your application using:

using Microsoft.Win32;

at the top of the file.

code and its description



The registry key is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to all users and HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to the current user, which contains entries for the softwares to be loaded at startup

in this example i have added a checkbox to be checked or unchecked if the user wants to add or remove application from startup, respectively. and a button to process the selection.


private void BtnAddRemove_Click(object sender, EventArgs e)
{
//to have access to HKEY_LOCAL_MACHINE and if u need to add only for HKEY_CURRENT_USER,
//use CurrentUser instead of LocalMachine
RegistryKey rk = Registry.LocalMachine;
RegistryKey mykey;

//access the regitry key where you want to add your registry key
mykey= rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (chkAddRemove.Checked == true )
{
//check if same registry value exists or not ......and create only if it is not there
if (mykey.GetValue("myApp") == null)
{
mykey.SetValue("myApp", Application.ExecutablePath, RegistryValueKind.ExpandString);
}

//dislpay message box if registry is already there
else
{
MessageBox.Show("The entry already exists");
}
}

//delete if the user has uncked the checkbox....and the value exists in the registry
else
{
if (mykey.GetValue("myApp") != null)
mykey.DeleteValue("myApp");
}
}






Responses

Author: Initiotech    31 Jul 2008Member Level: Gold   Points : 0
Good Coding
Thanks

Regards Initiotech


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Adding ,Retreiving and Deleting Key in Registry using C#
Previous Resource: Renaming Recycle Bin
Return to Discussion Resource Index
Post New Resource
Category: Registry Operations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use