Creating Captcha
The following code shows you how to generate a random string and creating the random folder of that string at the mentioned path.
randomString() function generates a random string and creates the folder of that string also.
So
you see, every time run this code it would generate a random string and
also create a folder of that generated random string on your mentioned
path.
Shutdown , LogOff and Restart Commands :
The following code shows you how to encode a data and then write back to file.
try
The following code shows you how to read a encoded file, with its corresponding encoded write method.
The following code shows you how to search in Listbox, first loading all words from file to listbox, then user enter word in textbox, and every key press word is searched and that word is displayed on image in small and capital letters.
}
The following code shows you dragging and dropping an image on form and setting that image the background of the form... using drag drop event of form. And also shows the path of dragged image.
Console.WriteLine(sum); // output : 10
The following code shows you how to generate a random string and creating the random folder of that string at the mentioned path.
randomString() function generates a random string and creates the folder of that string also.
Code
public void randomString()
{
StringBuilder builder = new StringBuilder();
Random rand = new Random();
char ch;
Here, also getting random size of that string which is to be generated, basically it is a length of that string.
int size = rand.Next(10,18);
for (int i = 0; i < size; i++)
{
Now,
here converting every integer value that is generated by the random
function to its corresponding ASCII value characters. Capital 'A' to small 'z'
ch = Convert.ToChar(rand.Next(65, 122));
Now , appending each character and make a string.
builder.Append(ch);
}
Now , creates the folder of that random string at your mentioned drive path.
Directory.CreateDirectory(@"D:\" + builder);
}
output of the program is (First Time Execution):
FLAiDbKvOj
output of the program is (Second Time Execution):
eDthvXecQcWxhrq
Text To Speech Converter
The following program shows you , text to speech converter in two ways.. old way using SpeechLib; and in new way
using System.Speech.Synthesis; you can also load whole file to read.
public void btnSpeak_Click(System.Object sender, System.EventArgs e)
{
textToSpeechNew();
}
public void textToSpeechOld() // Old way using using SpeechLib Library
{
SpVoice speech = new SpVoice();
speech.Speak(RichTextBox1.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
speech.Rate = 5;
speech.Volume = 50;
}
public void textToSpeechNew() // New way using System.Speech Library
{
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.Speak(RichTextBox1.Text);
speech.Rate = 5;
speech.Volume = 50;
}
public void btnLoadFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
TextReader tr = File.OpenText(openFileDialog1.FileName);
RichTextBox1.Text = tr.ReadToEnd();
textToSpeechNew();
}
}
Shutdown,LogOff and Restart Commands(C#.net)
Shutdown , LogOff and Restart Commands :
The following code shows you how you can send shutdown, logoff and restart commands to the computer.
Code
using System.Diagnostics;
public void action(string str)
{
switch (str)
{
case "LogOff":
Process.Start("shutdown", "-l -f -t 0");
break;
case "ShutDown":
Process.Start("shutdown", "-s -f -t 0");
break;
case "Restart":
Process.Start("shutdown", "-r -f -t 0");
break;
default:
break;
}
}
Reading and Writing Encoded Files
Encode A Data And Write In File (C#.net)
The following code shows you how to encode a data and then write back to file.
using System.IO;
using System.Text;
try
{
StreamWriter UTF7 = new StreamWriter("UTF7.txt", false, Encoding.UTF7);
UTF7.WriteLine("Hello World!!!");
UTF7.Close();
StreamWriter UTF8 = new StreamWriter("UTF8.txt", false, Encoding.UTF8);
UTF8.WriteLine("Hello, World!, its maverick");
UTF8.Close();
StreamWriter Unicode = new StreamWriter("Unicode.txt", false, Encoding.Unicode);
Unicode.WriteLine("bye bye , cruel world.....");
Unicode.Close();
StreamWriter UTF32 = new StreamWriter("UTF32.txt", false, Encoding.UTF32);
UTF32.WriteLine("mascot_ veracious wow, !not");
UTF32.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Reading A Encoded File (C#.net)
The following code shows you how to read a encoded file, with its corresponding encoded write method.
using System.IO;
using System.Text;
try
{
StreamReader UTF7 = new StreamReader("UTF7.txt", Encoding.UTF7);
string getUTF7Data = UTF7.ReadToEnd();
UTF7.Close();
StreamReader UTF8 = new StreamReader("UTF8.txt", Encoding.UTF8);
string getUTF8Data = UTF8.ReadToEnd();
UTF8.Close();
StreamReader Unicode = new StreamReader("Unicode.txt", Encoding.Unicode);
string getUnicodeData = Unicode.ReadToEnd();
Unicode.Close();
StreamReader UTF32 = new StreamReader("UTF32.txt", Encoding.UTF32);
string getUTF32Data = UTF32.ReadToEnd();
UTF32.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Making Your Own Task Manager (C#.net)
The following code shows you how to make
your own task manager, all the processes are in listbox and you can end
them also... or also you can add a new task as well.
using System.Diagnostics;
public void frmTaskManager_Load(System.Object sender, System.EventArgs e)
{
foreach (Process process in Process.GetProcesses())
{
listBoxProcesses.Items.Add(process.ProcessName);
}
}
public void btnEndTask_Click(System.Object sender, System.EventArgs e)
{
try
{
foreach (Process process in Process.GetProcessesByName(listBoxProcesses.SelectedItem.ToString()))
{
DialogResult dr = MessageBox.Show("You Want To End This Process?", "Decision?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
process.Kill();
int index = listBoxProcesses.SelectedIndex;
listBoxProcesses.Items.RemoveAt(index);
}
else
{
return;
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void btnNewTask_Click(object sender, EventArgs e)
{
Form frmNew = new frmNewTask();
frmNew.Show();
}
public void btnStart_Click(System.Object sender, System.EventArgs e)
{
try
{
Process.Start(txtNewTask.Text);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void btnBrowse_Click(System.Object sender, System.EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Title = "Please Select a File";
if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
{
txtNewTask.Text = OpenFileDialog1.FileName;
}
}
Searching In ListBox (C#.net)
The following code shows you how to search in Listbox, first loading all words from file to listbox, then user enter word in textbox, and every key press word is searched and that word is displayed on image in small and capital letters.
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
// This Function Load All Words From File To ListBox
public void LoadWordsFromFile()
{
try
{
TextReader tr = File.OpenText("abc.txt");
while (tr.ReadLine() != null)
{
ListBox1.Items.Add(tr.ReadLine());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Form1_Load(System.Object sender, System.EventArgs e)
{
try
{
LoadWordsFromFile(); //load allWords from file to listbox
Timer1.Interval = 100;
Timer1.Start();
image = new Bitmap("image.bmp");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// This Function Writes Text On Image At Specified Location
public Bitmap AddTextToImage(Bitmap bImg, string smallLetters, string capitalLetters)
{
Bitmap tmpImage = new Bitmap(bImg, new Size(bImg.Width, bImg.Height));
Graphics graphic = Graphics.FromImage(tmpImage);
SolidBrush brush = new SolidBrush(Color.Black);
graphic.DrawString(smallLetters, new Font("Times New Roman", 12, FontStyle.Bold), brush, new PointF(20, 25));
graphic.DrawString(capitalLetters, new Font("Times New Roman", 12, FontStyle.Bold), brush, new PointF(20, 50));
graphic.Dispose();
return tmpImage;
}
// At every key you press in textbox it finds that word in Listbox and select it
public void Timer1_Tick(System.Object sender, System.EventArgs e)
{
searchString = TextBox1.Text;
int index = ListBox1.FindString(searchString);
if (index != -1)
{
PictureBox1.Image = AddTextToImage(image, searchString.ToLower(),
searchString.ToUpper());
ListBox1.SetSelected(index, true);
}
public void btnSearch_Click(System.Object sender, System.EventArgs e)
{
try
{
if (TextBox1.Text.Equals(""))
{
ErrorProvider1.SetError(TextBox1, "Must Enter In TextBox");
}
else
{
ErrorProvider1.Dispose();
int index = ListBox1.FindString(TextBox1.Text);
ListBox1.SetSelected(index, true);
PictureBox1.Image = AddTextToImage(image, TextBox1.Text.ToLower(),
TextBox1.Text.ToUpper());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Drag And Drop Image On Form (C#.net)
The following code shows you dragging and dropping an image on form and setting that image the background of the form... using drag drop event of form. And also shows the path of dragged image.
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] list = (string[])e.Data.GetData(DataFormats.FileDrop,false);
Image img = Image.FromFile(list[0]);
this.BackgroundImage = img;
this.BackgroundImageLayout = ImageLayout.Center;
foreach (string file in list)
this.label1.Text += file + "\n";
}
}
Reactions: |
Adding Your Own Method To int , string classes (C#.net)
Extension Methods
The
following code shows you how to make extension methods, how to use
them. You can make your own method and add it to int, string library and
can use it wherever you want.
You
must define the method to be static and also the class in which this
method is implemented should also be static class. A very cool feature
of c# 4.0
Code
public static class ExtensionMethodsExample
{
// Adding a WordsCount method to String Class
// this function split the string at every space and then returns how many words string have
public static int WordsCount(this string str)
{
return str.Split(new char[] { ' ' }).Length;
}
public static void Main(string[] args)
{
string st = "i proud to be a pakistani";
Console.WriteLine(st.WordsCount()); // output : 6 (excluding of spaces)
}
// Adding a Tell method to int Class
// this function returns whether the number is even or odd
public static string Tell(this int no)
{
return no % 2 == 0 ? "Even" : "Odd";
}
public static void Main(string[] args)
{
int n = 5;
Console.WriteLine(n.Tell()); //output : Odd
int n = 4;
Console.WriteLine(n.Tell()); //output : Even
}
// Adding a Factorial method to int Class
// this function returns factorial of number
public static double Factorial(this int num)
{
double factorial = 1;
for (int i = 1; i <= num; i++)
factorial = factorial * i;
return factorial;
}
public static void Main(string[] args)
{
int n = 5;
Console.WriteLine(n.Factorial()); //output : 120
}
// Adding a ToInteger() method to string Class
// this function returns the given string converted to integer
public static int ToInteger(this string obj)
{
return int.Parse(obj);
}
public static void Main(string[] args)
{
string s = "1234";
int n = s.ToInteger(); // Converting string to integer
}
// Adding a ToInteger() method to char Class
// this function returns the given char converted to integer
public static int ToInteger(this char obj)
{
return int.Parse(obj.ToString());
}
public static void Main(string[] args)
{
string s = "1234";
int sum = 0;
for (int i = 0; i < s.Length; i++)
{
sum += s[i].ToInteger();// converting each char to integer and sum elements of string.
}
}
No comments:
Post a Comment