Friday, April 10, 2009

A Simple password generator

Here is a simple password generator.
there is nothing special in this code, i have just used the Random class in C# to generate random number and some range of character from Character Array, below code illustrate is more easily.

public string GetRandomPassword(int numChars , int seed)
{
string[] chars = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"0", "1", "2" ,"3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N","O","P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z" };

Random rnd = new Random(seed);
string random = string.Empty;
for(int i = 0 ; i < numChars ; i++)
{
random += chars[rnd.Next(0 , 62)];
}
return random;
}

Call to the Method above

Random random = new Random();
string Password = GetRandomPassword(15 , random.Next(100000))); //15 character long password

Wednesday, April 8, 2009

Late binding in C#

There are situations where some time it is viable to go for late binding.
though it is a tedious to perform and also needs carefull overall programming.

I was working on a project where clients were continious asking for new changes and want in Auto updates and at that time found late binding better for the situation.

Though early binding is considered as better option than late binding, but late binding has its own benefits. Late bind is most of used for generic object which needs to be known a run time.

in order to illustrate i have created of class library called Calculation which contains Calculate Class. code of that is below:


using System;
using System.Collections.Generic;
using System.Text;

namespace Calculation
{
public class Calculate
{
public Calculate()
{
}

public Int64 Add(Int64 Value1 , Int64 Value2)
{
return Value1 + Value2;
}

public Int64 Multiply(Int64 Value1 , Int64 Value2)
{
return Value1 * Value2;
}
}
}


in order to use this Calculation.dll at run time don't don't make a reference to Calculation.dll. i have placed the dll on the Startup path of my test application.
code for that is listed below:
do not forget to make a reference to System.Reflection

private void button1_Click(object sender , EventArgs e)
{
Assembly assemblyinfo = Assembly.LoadFile(Application.StartupPath+@"\Calculation.dll");
Type[] types = assemblyinfo.GetTypes();

foreach(Type type in types)
{
MethodInfo[] MI = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
object Latebind = Activator.CreateInstance(type);
MessageBox.Show(MI[0].Invoke(Latebind , new object[] { Convert.ToInt64(textBox1.Text),Convert.ToInt64(textBox2.Text)}).ToString());
}
}

here MI[0] referes to Add Method.
you can check the Method order by itterating through MI object

Friday, April 3, 2009

FTP Upload/Download using C#

Here is a very simple method for uploading and downloading files from FTP server.
you need to have FTP Host Name and UserName and Password for Authentication credentials

Make a reference to System.Net

DownLoad:

//Filepath is the path to your local machine where you want to download the file
//Filename is the name of the file which you want to download from FTP Server
private void Download(string filePath, string fileName,string UserName,string Password)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtPath.Text + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(UserName,Password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}

ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


Upload:

//Filename is the file from your local machine to upload
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = FTPPath+ "/" + fileInf.Name; //FTPPath points to the directory on FTP Server where you want to upload file
FtpWebRequest reqFTP;

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(txtUserName.Text, txtpassword.Text);
reqFTP.KeepAlive = false;

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;

reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}

Hope this will be helpful to you. some of codes i have taken from Code Project