Design and Implementation of the WinForm Application Registration Module

Source: Internet
Author: User

When installing some desktop applications, we often prompt that the current user is using a trial version to register. I just recently made a demo about Application Registration (here is just a line of thinking). I will share it with you here.
The project requirements are as follows:
1. Obtain the host hard disk serial number, Nic MAC address, and CPU number, and generate the machine code according to certain algorithms.
2. The registration code can be generated according to certain algorithms based on the machine code
3. When you are not registered, you cannot use the member management function.
Let's get started right away. We can also demonstrate how to implement it step by step through examples.
1. Create a Winform application, and create three new Form forms: Form1 (LOGIN), Register (registration), and Form2 (main Form)
The interface layout is as follows:

 


2. First, obtain the host hard drive serial number, Nic MAC address, and CPU number, generate the machine code based on certain algorithms, and then generate the registration code based on the machine code. (In order to facilitate your use, I have encapsulated it as a class and exported a class template to download registernumber.zip)
This class has the following method:
1. GetMac () // obtain the MAC address of the NIC
2. GetHD () // obtain the serial number of the hard disk
3. GetCPU () // obtain the CPU ID
4. GetMachineNumber () // generates a machine code based on the obtained MAC address, hard disk serial number, and CPU number.
5. GetRegisterNumber () // generate a registration code based on the generated machine code
The method mentioned above has specific Declaration implementation in the RegisterNumber class. If you need it, please download it and use it by yourself. I will not explain it here, but I will explain it clearly.
3. perform the following operations in the Form1, that is, the Form_Load event of the login form:
[Csharp]
Private void Form1_Load (object sender, EventArgs e)
{
SkinEngine1.SkinFile = "WarmColor1.ssk"; // load files for skin controls
RegistryKey rk = Registry. LocalMachine. OpenSubKey ("SOFTWARE"); // open the Registry key of the system. Registry. LocalMachine contains the configuration data of the local computer. This field reads the Windows Registry key HKEY_LOCAL_MACHINE. OpenSubKey ("SOFTWARE") indicates that the SOFTWARE subitem under the Local Computer Configuration data is enabled.
If (rk. GetValue ("ZCM ")! = Null) // obtain the value of the registration code of the program. If it has been registered, there is a value. The Registration button is hidden. If it is not registered, the value is null. The Registration button is displayed.
{
Btnzc. Visible = false;
}
}
The Registration button event handler function is as follows:

[Csharp]
Private void btnzc_Click (object sender, EventArgs e)
{
Register r = new Register (); // display the Registration Form for Registration
R. Show ();
}
The logon button event processing function is as follows:

[Csharp]
Private void button#click (object sender, EventArgs e)
{
This. dialogResult = DialogResult. OK; // here is only a demo, so you can log on successfully by clicking login. Here, the value of DialogResult is set to OK, And the Form2 main form is not directly displayed, after successful logon, The Form1 login form is useless, so when the Program is running, the main thread is not Form1 but From2. in the cs file.
}
Set the following in the Program. cs file:

[Csharp]
Static void Main ()
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
<Span style = "background-color: rgb (255,255, 0)"> Form1 f1 = new Form1 ();
If (f1.ShowDialog () = DialogResult. OK) // indicates that when the DialogResult of f1 is OK, the main program starts to run. Therefore, set Dialogresult to OK when the Form1 log on is successful.
{
Application. Run (new Form2 ());
}
</Span>}
4. If you want to Register, let's take a look at the code in the Registration Form (Register ).

1. First, check the method in the Register_Load event.
[Csharp]
Private void Register_Load (object sender, EventArgs e)
{
Txtzcm. Text = RegisterNumber. GetRegisterNumber (); // call the static method for obtaining the registration code of RegisterNumber to obtain the registration code of the current machine. No value is assigned to the Text box.
// FileStream fs = new FileStream (Application. StartupPath + "\ zcm.txt", FileMode. Create, FileAccess. Write); // Write the registration code to the zcm.txt file in the startup Item of the program
// StreamWriter sw = new StreamWriter (fs, Encoding. Default );
// Sw. Write (txtzcm. Text );
// Sw. Close ();
// Fs. Close ();
}
2. Check the event handling in the Registration button.
[Csharp]
Private void button#click (object sender, EventArgs e)
{
If (! String. IsNullOrEmpty (txtsr. Text) // checks whether user input is empty.
{
If (txtsr. Text = txtzcm. Text) // check whether the user input is consistent with the displayed registration code
{
RegistryKey rk = Registry. LocalMachine. OpenSubKey ("SOFTWARE", true );
Rk. SetValue ("ZCM", txtsr. Text); // write the registration code to the "ZCM" name in the SOFTWARE file under the Registry about program information of the system
MessageBox. Show ("registration successful! Log on again! ");
}
Else
{
MessageBox. Show ("the registration code you entered is incorrect. Please enter it again! ");
}
}
Else
{
MessageBox. Show ("Enter the registration code! ");
}
}
5. Next we will enter the most critical part. Whether or not the user is registered, the user can access the main interface of the program, however, if a user is not registered, the user determines whether the number of user attempts has exceeded the limit. If the number of user attempts has exceeded the limit, the user cannot use it. Otherwise, the user can continue the trial, the user is reminded of the remaining number of trial times during each use. If you have already registered, you can obtain all the permissions. The following describes the specific code implementation.
[Csharp]
Private void Form2_Load (object sender, EventArgs e)
{
RegistryKey rk = Registry. LocalMachine. OpenSubKey ("SOFTWARE", true );
If (rk. GetValue ("ZCM ")! = Null) // read the registration code of the system. If the value is not null, you can use it to identify that it has been registered. Otherwise, it is not registered.
{
This. Text = "registered ";
Return;
}
This. Text = "trial version (not registered )";
Int count = (int) rk. GetValue ("Count", 0); // The number of times the user tried it when it was not registered. If it was used for the first time, the value is 0.
MessageBox. Show ("Thank you for using" + count + "times. Please register before using it! ");
Tabc. Visible = false; // because the version is used, many functions cannot be used. Here we demonstrate setting the visible attribute of tabControl to false to demonstrate this effect.
If (count <30) // The default number of trials is 30. if the number is less than 30, you can still try it. Otherwise, you can stop running it directly.
{
Rk. setValue ("Count", count + 1); // you need to update the value in the Registry to record the number of logins. This value is increased by one for each trial. When the value is greater than or equal to 30, exit directly after the trial is completed.
Count = 30-count;
MessageBox. Show ("You are trying a trial version! There are also "+ count +" trial opportunities. Please register before using it! ");
}
Else
{
MessageBox. Show ("sorry, please have exceeded the number of trials! Please register and use it again! ");
Application. Exit ();
}
}
Now, the design and implementation of the Program registration module have been completed. We can basically implement a simple program registration, here we mainly use the Registry class and RegistryKey class to save the registration code of the system and the number of logins. I will introduce these two classes below. RegistryKey is located in the Microsoft. Win32 namespace.
The registry key is the basic organization unit in the registry, which is equivalent to a folder in Windows Resource Manager. Each specific registry item can have a subitem. As long as the user has the corresponding permissions and the Registry item is not the base item or the next level of the base item, the Registry item can be deleted. Each registry key can also contain multiple values associated with it, which are used to store information. Generally, we can create a RegistryKey (HKEY_LOCAL_MACHINE \ Software) to save information about the applications installed on the computer. For example, in this example, we use this method to save the user trial times and the machine registration code. Below are several common primary keys:
HKEY_CLASSES_ROOT this primary key contains the file extension and application association information, and Window Shell and OLE are used to store registry information. The subkeys under the primary key determine how to display the files and their icons in WINDOWS. The primary key is mapped from HKEY_LCCAL_MACHINE \ SOFTWARE \ Classes.
HKEY_CURRENT_USER this primary key contains information about the current user, such as user window information and desktop settings.
The HKEY_LOCAL_MACHINE primary key contains the installation and configuration information of computer software and hardware,
This information is available for all users to use HKEY_USERS. This primary key records the settings of the current user. Every time a user logs on to the system, a subkey with the same name as the user login name is generated under this primary key, which stores information such as the current user's desktop settings, background bitmap, shortcut keys, and font. Generally, applications do not directly access and change the primary key, but access through the primary key HKEY_CURRENT_USER.
HKEY_CURRENT_CONFIG the primary key stores the configuration information of the current hardware of the computer. These configurations can be changed based on the network type currently connected or the hardware driver software installation changes.
In the program, four methods in the RegistryKey class are used to read the subkeys and their values under the specified Primary Key: OpenSubKey, GetSubKeyNames, GetValueNames, and GetValue. The specific usage and meaning are as follows: The OpenSubKey (string name) method mainly enables the specified subkey. The GetSubKeyNames () method is used to obtain the names of all subkeys under the primary key. The returned value is a string array. The GetValueNames () method is used to obtain all the key names in the current subkey, and its return value is also a string array. The GetValue (string name) method specifies the key value of a key.
These methods are used in this example. The following is an example:
To facilitate use, the Registry class provides seven public static domains, representing seven basic primary keys respectively: Registry. classesRoot, Registry. currentUser, Registry. localMachine, Registry. users, Registry. currentConfig here we use Registry. localMachine
For demonstration.
[Csharp]
RegistryKey rk = Registry. localMachine. openSubKey ("SOFTWARE", true "); // There are two parameters: the first parameter is the folder to be opened, and the second parameter is the write permission specified for it.
RegistryKey rk1 = rk. CreateSubKey ("Hello", RegistryKeyPermissionCheck. ReadWriteSubTree); // create a subitem under SOFTWARE. Hello, the second parameter is Enumeration type, which allows reading and writing
Rk. SetValue ("Count", 0); // set a key-Value Pair in SOFTWARE to save the number of logins
Rk. GetValue ("Count", 0); // obtain the Count value. If the current key-Value Pair does not exist, the default value is 0.
Rk. DeleteValue ("Count"); // delete a key-Value Pair named "Count"
Rk. DeleteSubKey ("Hello"); // Delete the subitem Hello under SOFTWARE
A careful friend may find that there are some annotated code in the above Load event in Register (registration form) as follows:
[Csharp] www.2cto.com
FileStream fs = new FileStream (Application. StartupPath + "\ zcm.txt", FileMode. Create, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs, Encoding. Default );
Sw. Write (txtzcm. Text );
Sw. Close ();
Fs. Close ();
This code is written into the zcm.txt file under the executable file of the application program. This is also an idea to check whether a user is registered. When a user is registered, the registration code is written into the text file, when Logging On, you can read the registration code from the file by opening or creating a new one. If you have already registered, you can read the registration code. If you have not registered, the system displays unregistered information and prompts you to register.
Here is just a way of thinking, hoping to help you, and please give more advice.

Related Article

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.