This article will show you how you can generate a string with
specific length using C#.net and linq. This article example I am showing in
windows application.
Some of my previous articles are as follows: Strong
Random String Password Generation Using C#.Net,Login
Form By UserType OR User Role In Windows Application In C#.Net , Linq,
Display
Message With Warning icon in Windows Application Using C#.Net, Display
ModalPopupExtender on Page Load In Asp.Net Using AjaxControlToolkit, C#.Net,
Add
Select text in Dropdownlist in Asp.Net OR Adding Default Select Option in the
DropDownList Using C#.Net in Asp.Net, Search
and Display Data In GridView From Database Table In Asp.Net Using C#.Net.
So for this article first we will create a
new windows application and add the below code on button click event.
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 Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
textBox1.Text = RandomString(Convert.ToInt32(textBox2.Text));
}
///
/// Function to generate
random string
///
|
///
///
public string
RandomString(int NoofCharacter)
{
string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random r = new Random();
string randonpassword = new
string(
Enumerable.Repeat(characters,
NoofCharacter)
.Select(s => s[r.Next(s.Length)])
.ToArray());
return randonpassword;
}
}
}
In above code I have created a function named randomstring and
in this I have passed the length of the random string which we are going to
generate.
Now on button click event call the method to generate the
password. Now we have done run the application to check the output.
0 comments:
Please let me know your view