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: