Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to get the license key in windows application. And i need 2PHXF-9PBDW-D3WWY-CPDKD-XG87V this format. Please help me....
i have tried this code


C#
private void btn_License_Click(object sender, EventArgs e)
       {
           Random ran = new Random();
           textBox1.Text = "" + ran.Next(0, 9) + ran.Next(0,9) + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9) + "_" + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9) + "_" + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9) + ran.Next(0, 9);
       }
Posted
Updated 8-Jul-13 18:48pm
v2
Comments
Sushil Mate 9-Jul-13 0:49am    
Do you know how to create installer? This is same question you have posted here.

My Soltion is:

private static Random rd = new Random((int)DateTime.Now.Ticks);
C#
private void btn_License_Click(object sender, EventArgs e)
       {

           string str1 = GeneratePassword(5, 0);
           string str2 = GeneratePassword(5, 0);
           string str3 = GeneratePassword(5, 0);
           string str4 = GeneratePassword(5, 0);
           string str5 = GeneratePassword(5, 0);
           textBox1.Text = str1 + "-" + str2 + "-" + str3 + "-" + str4 + "-" + str5;

C#
}

       public static string GeneratePassword(int Lenght, int NonAlphaNumericChars)
       {

           string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
           string allowedNonAlphaNum = "!@#$%^&*()_-+=[{]};:<>|./?";
           //Random rd= new Random((int)DateTime.Now.Ticks);

           if (NonAlphaNumericChars > Lenght || Lenght <= 0 || NonAlphaNumericChars < 0)
               throw new ArgumentOutOfRangeException();

           char[] pass = new char[Lenght];
           int[] pos = new int[Lenght];
           int i = 0, j = 0, temp = 0;
           bool flag = false;

           //Random the position values of the pos array for the string Pass
           while (i < Lenght - 1)
           {
               j = 0;
               flag = false;
               temp = rd.Next(0, Lenght);

               for (j = 0; j < Lenght; j++)
                   if (temp == pos[j])
                   {
                       flag = true;
                       j = Lenght;
                   }

               if (!flag)
               {
                   pos[i] = temp;
                   i++;
               }
           }

           //Random the AlphaNumericChars
           for (i = 0; i < Lenght - NonAlphaNumericChars; i++)
               pass[i] = allowedChars[rd.Next(0, allowedChars.Length)];

           //Random the NonAlphaNumericChars
           for (i = Lenght - NonAlphaNumericChars; i < Lenght; i++)
               pass[i] = allowedNonAlphaNum[rd.Next(0, allowedNonAlphaNum.Length)];

           //Set the sorted array values by the pos array for the rigth posistion
           char[] sorted = new char[Lenght];
           for (i = 0; i < Lenght; i++)
               sorted[i] = pass[pos[i]];

           string Pass = new String(sorted);

           return Pass;
       }





Result is:Y4CDQ-GAIW5-VIWE1-0EFN6-C5Z7H
 
Share this answer
 
v3
Here is the C# code.Just click on button and you will get different license keys

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Random random = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s1 = RandomString();
            string s2 = RandomString();
            string s3 = RandomString();
            string s4 = RandomString();
            string s5 = RandomString();
            textBox1.Text = s1+" - "+s2+" - "+s3+" - "+s4+" - "+s5;
        }

      public string RandomString()
        {
            string input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < 5; i++)
            {
                ch = input[random.Next(0, input.Length)];
                builder.Append(ch);
            }
            return builder.ToString();
        }
    }
}


Check out and let me know if it helped
 
Share this answer
 
v2
Comments
Rajan from Thiruvananthapuram 9-Jul-13 5:18am    
Use this line in ur pgm.

private static Random rd = new Random((int)DateTime.Now.Ticks);
it gives distinct string.
for eg:
Y4CDQ-GAIW5-VIWE1-0EFN6-C5Z7H like this..
Rajan Maheshwari 9-Jul-13 5:34am    
the RandomString() function is called 5 times and values are stored in s1,s2,s3,s4,s5 respectively...it will also provide a single DISTINCT concatenated string.
Its not necessary to use private static Random rd = new Random((int)DateTime.Now.Ticks);
Check out..
Rajan from Thiruvananthapuram 9-Jul-13 5:39am    
i have already checked that.. bt the result is:
Y4CDQ-Y4CDQ-Y4CDQ-Y4CDQ-Y4CDQ like this..
Rajan Maheshwari 9-Jul-13 5:41am    
no dude..its working fine in my pc...if u don't believe I can send a video to ur id
Rajan from Thiruvananthapuram 9-Jul-13 5:44am    
i accept this answer..
sorry for my misunderstanding...
just make 5 labels.
Make a char randomize function.You can find the randomize function of characters of 5 digits on net
At last concatenate the string of all the labels and store in a database and then if you wish you can print in a label or a textbox and you are done..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900