Saturday, April 17, 2010

Generate GUID C#

System.Guid guid = System.Guid.NewGuid() ;
String id = guid.ToString();

Find IP Address in Simple way C#

string strHostName = System.Net.Dns.GetHostName();// Get Host name.

string myIP = System.Net.Dns.GetHostByName(strHostName).AddressList[0].ToString();

Tuesday, April 13, 2010

Create Directory FTP C#

private void MakeDir(string dirName)
{
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();

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

FTP upload

private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;

// Create FtpWebRequest object from the Uri provided
// reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

reqFTP = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));//.Create(sURI);//"new Uri("\\AGD14\\Archival")");

// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;

// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// Specify the data transfer type.
reqFTP.UseBinary = true;

// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;

// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();

try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();

// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);

// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}

// Close the file stream and the Request Stream
strm.Close();
fs.Close();
MessageBox.Show("File Uploaded");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}

loop through folders or directories including sub folders

public static void ProcessDir(string sourceDir/*, int recursionLvl*/)
{

string sTemp = sDestinationFolder;
// sw8.WriteLine("**********" + sourceDir + "***********");

// if (recursionLvl <= 5)
// {
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
sTemp = fileName.Remove(0, sDestinationFolder.Length + 1);
lstSource.Add(sTemp);
WriteLogFile(sTemp, MESSAGE_TYPE.INFO);
// do something with fileName
Console.WriteLine(fileName);
}

// Recurse into subdirectories of this directory.
string[] subdirEntries = Directory.GetDirectories(sourceDir);
foreach (string subdir in subdirEntries)
{
sTemp = subdir.Remove(0, sDestinationFolder.Length + 1);
WriteLogFile(sTemp, MESSAGE_TYPE.INFO);
lstSource.Add(sTemp);

// Do not iterate through reparse points
if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)

ProcessDir(subdir/*, recursionLvl + 1*/);
}
// }
}

Loop through FTP folder tree

public static void ProcessFiles(string serverUri, string folder)
{
string targetDir = targetFolder + @"\" + folder;

if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);

FtpWebRequest reqFilename = (FtpWebRequest)WebRequest.Create(serverUri);
reqFilename.Credentials = nc;
reqFilename.Method = WebRequestMethods.Ftp.ListDirectory;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Credentials = nc;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

try
{

//FtpWebResponse resFilename = (FtpWebResponse)resFilename.GetResponse();
FtpWebResponse resFilename = (FtpWebResponse)reqFilename.GetResponse();
StreamReader file = new StreamReader(resFilename.GetResponseStream());

string sfileLine;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader dir = new StreamReader(response.GetResponseStream());
List subdir = new List();
Regex rgdir = new Regex("");
//Regex rgfile = new Regex(@"(\b\S*)$");
Regex rgfile = new Regex(@"(\b*)$");
string aline;
string filename;
while (!dir.EndOfStream)
{
aline = dir.ReadLine();
sfileLine = file.ReadLine();
// filename = aline.Substring(aline.LastIndexOf("DIR") + 4).Trim();//rgfile.Match(aline).Groups[0].Value.Trim();
if (rgdir.Match(aline).Success)
{
filename = sfileLine;//aline.Substring(aline.LastIndexOf("DIR") + 4).Trim();//rgfile.Match(aline).Groups[0].Value.Trim();
subdir.Add(filename);
}
else
{
filename = sfileLine;//rgfile.Match(aline).Groups[0].Value.Trim();
}

// WriteLogFile(filename, MESSAGE_TYPE.INFO);
if (folder != null)
{
lstDest.Add(folder + @"\" + filename);
WriteLogFile(folder + @"\" + filename, MESSAGE_TYPE.INFO);
}
else
{
lstDest.Add(filename);
WriteLogFile(filename, MESSAGE_TYPE.INFO);
}
/* if (filename.Substring(filename.Length - 4).ToUpper() == ".ZIP")
{
// Console.WriteLine(folder + @"\" + filename);
WriteLogFile(folder + @"\" + filename, MESSAGE_TYPE.INFO);
// DownloadOneFile(serverUri + "/" + filename, targetDir + @"\" + filename);
// DeleteOneFile(serverUri + "/" + filename);
}*/
}
string sFolderName;
foreach (string s in subdir)
{

if(folder != null)
sFolderName = folder + @"\" + s;
else
sFolderName = s;

//ProcessFiles(serverUri + "/" + s, folder + @"\" + s);

ProcessFiles(serverUri + "/" + s, sFolderName);
}
resFilename.Close();
response.Close();
dir.Close();

}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
}

File Size using FileInfo

 FileInfo f = new FileInfo(fileName);
long s1 = f.Length;

FTP file size

static NetworkCredential nc = new NetworkCredential("user", "password");

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("ftp://" + ftpServerIP + "/" + sSource));

//(FtpWebRequest)WebRequest.Create(("ftp://ftp.domain.com/doesntexist.txt");
request.Credentials = nc;
request.Method = WebRequestMethods.Ftp.GetFileSize;

Monday, April 5, 2010

what's is the difference between reference and pointers?

File Compress C#

using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and already compressed files.
if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".zip")
{
// Create the compressed file.
using (FileStream outFile = File.Create(fi.FullName + ".zip"))
{

using (GZipStream Compress = new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into the compression stream.
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
{
Compress.Write(buffer, 0, numRead);
}
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}