Main idea: Get the CPU serial number of the machine, get machine code. The registration code is obtained by MD5 operation of the machine code. Write the registration code into the registry or system directory.
1. At the entrance of the program (WinForm) detect the existence of the registration file, if it does not exist
Prompts the user to register file corruption or the user is not registered, if the registration file exists, read the contents of the file, to determine whether the registration code is correct.
The file operation is done by referencing the using System.IO in the file where the entry function of the program is located.
- Using System;
- Using System.Collections.Generic;
- Using System.Windows.Forms;
- Using System.IO;
- Namespace Loginwindowtest
- {
- Static Class Program
- {
- <summary>
- The main entry point for the application.
- </summary>
- [STAThread]
- static void Main ()
- {
- String sysfolder = system.environment.systemdirectory;//Get system installation directory such as: C:\Windows\System32
- Reg RG=NEW Reg ()//There are several functions in this class (get the CPU serial number, MD5 the string, etc.)
- Application.enablevisualstyles ();
- Application.setcompatibletextrenderingdefault (FALSE);
- if (! File.exists (sysfolder + "file://sixi.ini/"))
- {
- If the registration file does not exist. Registration failed
- Create a registration file
- File.create (sysfolder + "file://sixi.ini/");
- MessageBox.Show ("Please Register!") ");
- Application.Run (New RegForm ());//Enter the registration interface.
- }
- Else
- {//If the registration file exists, read the file content to compare with the password
- byte[] Arry=new byte [32];
- String str = "";
- FileInfo fi = new FileInfo (sysfolder + "file://sixi.ini/");
- FileStream fs = fi. OpenRead ();
- int I=fs. Read (Arry, 0, 32);
- Fs. Close ();
- str = System.Text.Encoding.ASCII.GetString (Arry);
- if (str = RG.GETMD5 (RG). Getcpuid ()). Trim ())//If the string in the registration file is the same as the registration code that was MD5, the registration is successful
- {
- MessageBox.Show ("Software has been successfully registered!") ");
- Application.Run (New MainForm ());//Enter Software main interface
- }
- Else
- {
- MessageBox.Show ("Please register the software!") ");
- Application.Run (New RegForm ());//Enter the registration interface.
- }
- }
- }
- }
- }