Thursday, July 16, 2009

What is backgroundWorker in windows application?

Wednesday, July 15, 2009

Download and unzip files form FTP server in .Net desktop Application with C#

Here i am showing how to download files from FTP server and also how to unzip a zip file.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using ICSharpCode.SharpZipLib.Zip;
using System.Security;
using System.Security.Principal;
using System.Security.AccessControl;
namespace ftpAccess
{
public static class Download
{
static List SymbolLst;
static string savePath = string.Empty;

#region DownloadF File From FTP Server

public static bool DownloadFile(string fileSavePath, string DownloadfileName,
string ftpServerIP, string ftpUserID, string ftpPassword)
{
bool result = false;
SymbolLst = SymbolList(DownloadfileName);
savePath = fileSavePath;
for (int i = 0; i <>
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(fileSavePath + "\\" + Convert.ToString(SymbolLst[i]), FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + Convert.ToString(SymbolLst[i])));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 6048;
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);
result = false;
}
}
//ExtractFile();
if (unzipclickbank() == true)
{
DeleteOldZipFile();
result = true;
}
return result;
}
#endregion

#region Return all files to download
private static List SymbolList(string ConnSymbol)
{
string[] namesArray = ConnSymbol.Split(',');
List lstTest = new List(namesArray.Length);
lstTest.AddRange(namesArray);
lstTest.Reverse();
return lstTest;
}
#endregion

#region UnZip All Files
public static bool unzipclickbank()
{
try
{
for (int i = 0; i <>
{
//Checks if file is open
FileStream fs = new FileStream(savePath + "/" + Convert.ToString(SymbolLst[i]), FileMode.Open, FileAccess.ReadWrite);
fs.Close();
ZipInputStream s = new ZipInputStream(File.OpenRead(savePath + "/" + Convert.ToString(SymbolLst[i])));
s.Password = "ryancharts";
ZipEntry theEntry = default(ZipEntry);
theEntry = s.GetNextEntry();
while ((theEntry != null))
{
Console.WriteLine(theEntry.Name);
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);

//create directory
if (directoryName.Length > 0)
{
Directory.CreateDirectory(savePath + "/" + directoryName);
}
SetPermissions(savePath);
if (fileName != string.Empty)
{
FileStream StreamWriter = File.Create(savePath + "/" + theEntry.Name);

int Size = 6048;

byte[] data = new byte[6049];
while (true)
{
Size = s.Read(data, 0, data.Length);

if (Size > 0)
{
StreamWriter.Write(data, 0, Size);
}
else
{
break; // TODO: might not be correct. Was : Exit While
}
}
StreamWriter.Close();
}
theEntry = s.GetNextEntry();
}
}
}
catch (Exception ex)
{
return false;
}
return true;
}
#endregion

#region Delete Zip File
///
///
///
private static void DeleteOldZipFile()
{
for (int i = 0; i <>
{
FileInfo finfo = new FileInfo(savePath + "\\" + Convert.ToString(SymbolLst[i]));
if (finfo.Exists)
{
finfo.Delete();
finfo.Refresh();
}
}
}
#endregion
}
}