Let's take a look at the effect:
Recently I am working on a foreign trade import software, which is developed using the C/S architecture. For ease of use, this does not try to add the function of remembering the user name and password, and can be set to enable the software automatically upon startup, all in order to reduce the user's click operation.
There are multiple ways to save user information, such as saving it as a local file, xml, registry, and even worse, saving it to the database. I personally think this operation was a little bad before I logged on to the database. It is not good to go to the database to retrieve the password and put it back in the input box.
Finally, the Registry is used to save the information. In order to demonstrate that the password saved in the registry is not encrypted, you should continue to encrypt the password for security.
Private void ckbKeepInfo_CheckStateChanged (object sender, EventArgs e) {RegistryKey location = Registry. localMachine; RegistryKey soft = location. openSubKey ("SOFTWARE", true); // you can specify RegistryKey myPass = soft. createSubKey ("FTLiang"); myPass. setValue ("s1", tbUserName. text); myPass. setValue ("s2", tbPassword. text); myPass. setValue ("s3", ckbKeepInfo. checked );}
Automatic Login:
Private void ckbAutoStart_CheckStateChanged (object sender, EventArgs e) {if (ifFistIn = false) {RegistryKey location = Registry. localMachine; RegistryKey soft = location. openSubKey ("SOFTWARE", true); // you can specify RegistryKey myPass = soft. createSubKey ("FTLiang"); myPass. setValue ("s4", ckbAutoStart. checked); if (ckbAutoStart. checked) {string exeDir = Application. executablePath; // the absolute path of the program to be started. RegistryKey rk = Registry. localMachine; RegistryKey softWare = rk. openSubKey ("SOFTWARE"); RegistryKey microsoft = softWare. openSubKey ("Microsoft"); RegistryKey windows = microsoft. openSubKey ("Windows"); RegistryKey current = windows. openSubKey ("CurrentVersion"); RegistryKey run = current. openSubKey (@ "Run", true); // Add true here to get the write permission run. setValue ("FTStart", exeDir);} else {string exeDir = Application. executablePath; // the absolute path of the program to be started. RegistryKey rk = Registry. localMachine; RegistryKey softWare = rk. openSubKey ("SOFTWARE"); RegistryKey microsoft = softWare. openSubKey ("Microsoft"); RegistryKey windows = microsoft. openSubKey ("Windows"); RegistryKey current = windows. openSubKey ("CurrentVersion"); RegistryKey run = current. openSubKey (@ "Run", true); // Add true here to get the write permission run. deleteValue ("FTStart"); // you must add true here to get the write permission }}}
Initial form display:
Private void FmLogin_Load (object sender, EventArgs e) {// read from the Registry whether the user name and password are saved and automatically start configuration try {RegistryKey location = Registry. localMachine; RegistryKey soft = location. openSubKey ("SOFTWARE", false); // you can specify RegistryKey myPass = soft. openSubKey ("FTLiang", false); tbUserName. text = myPass. getValue ("s1 "). toString (); string s2 = myPass. getValue ("s2 "). toString (); bool ifSave = Convert. toBoolean (myPass. getValue ("s3"); ckbKeepInfo. checked = ifSave; bool ifSave2 = Convert. toBoolean (myPass. getValue ("s4"); ckbAutoStart. checked = ifSave2; if (ifSave) {tbPassword. text = s2;} else {tbPassword. text = "";} ifFistIn = false; // after the program is started, you can execute registry-related actions} catch (Exception ex) {// todo something }}