WinForm development control application self-starting function, winform Application

Source: Internet
Author: User

WinForm development control application self-starting function, winform Application

This article mainly describes how to set the auto-start function for WinForm application development, which is often involved in actual development and is very practical. It refers to the use of the registry to control whether the program is started on its own, the specific functions are shown in the previous two figures, which are more intuitive.
For example:

Implementation Code of program settings persistence Interface

Using Microsoft. win32; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. IO; using System. linq; using System. text; using System. windows. forms; namespace Tools. app {public partial class AutoRun: Form {public string KeyName = "Tools data export service program"; public AutoRun () {InitializeComponent ();} private void button#click (object sender, E VentArgs e) {try {string strName = Application. executablePath; // string strnewName = strName. substring (strName. lastIndexOf ("\") + 1); if (AutoMenu. checked) {this. autoMenu. checked = true; if (! File. exists (strName) // specifies whether the file has return; Microsoft. win32.RegistryKey Rkey = Microsoft. win32.Registry. localMachine. openSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); if (Rkey = null) {Rkey = Microsoft. win32.Registry. localMachine. createSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run");} Rkey. setValue (KeyName, strName + "-s"); // modify the registry so that the program runs automatically when it is started. MessageBox. Show ("the program has been set to be self-started. It takes effect after you restart the computer! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information);} else {// modify the registry so that the program is not automatically executed when it is started. This. autoMenu. checked = false; RegistryKey Rkey = Registry. localMachine. openSubKey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); if (Rkey = null) {Rkey = Microsoft. win32.Registry. localMachine. createSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run");} Rkey. deleteValue (KeyName, false); MessageBox. show ("the auto-start setting has been canceled! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information) ;}} catch // if you do not have the permission, {MessageBox. Show (" Please use the Administrator permission to open the application! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Error) ;}} private void AutoRun_Load (object sender, EventArgs e) {try {string strName = Application. ExecutablePath; if (! File. exists (strName) // specifies whether the file has return; Microsoft. win32.RegistryKey Rkey = Microsoft. win32.Registry. localMachine. openSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); if (Rkey! = Null) {string val = Rkey. getValue (KeyName ). toString (); if (val = (strName + "-s") {this. autoMenu. checked = true;} else {this. autoMenu. checked = false ;}} catch (Exception ex) // if you do not have the permission, an Exception occurs. {// MessageBox. show (ex. message); MessageBox. show ("Please open the application with the administrator privilege! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Error );}}}}

I hope the above information will be helpful to beginners. Thank you!
More follow Fu Yi Fang technology blog: http://blog.csdn.net/fuyifang
Or scan the QR code on your mobile phone to view more blog posts:

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.