Pages

Thursday, March 10, 2011

Read webpage using HttpWebRequest in asp.net screen scraping

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://yahoo.com/");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader s = new StreamReader(res.GetResponseStream(), Encoding.ASCII);
string str = s.ReadToEnd();

Monday, March 7, 2011

Upload download Delete and Retrieve File List from FTP ASP.net c#

using System.Net;
Retrieve File List from Local Directory:
string localPath = @"G:\FTPTrialLocalPath\";
string[] files = Directory.GetFiles(localPath);
foreach (string filepath in files)
{
  string fileName = Path.GetFileName(filepath);
  Console.WriteLine(fileName);
}

Retrieve File List from FTP Directory:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source");
request.Credentials = new NetworkCredential("username", "pwd");
request.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
string fileName = streamReader.ReadLine();
while (fileName != null)
{
 Console.Writeline(fileName );
 fileName = streamReader.ReadLine();
}
request = null;
streamReader = null;

Delete File From FTP:
string fileName = "abc.txt";
FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDelete.Credentials = new NetworkCredential("username", "pwd");
requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();

Upload File to FTP:
string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "abc.txt";
FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/Destination/" + fileName);
requestFTPUploader.Credentials = new NetworkCredential("username", "pwd");
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo fileInfo = new FileInfo(localPath + fileName);
FileStream fileStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
Stream uploadStream = requestFTPUploader.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
 uploadStream.Write(buffer, 0, contentLength);
 contentLength = fileStream.Read(buffer, 0, bufferLength);
}
uploadStream.Close();
fileStream.Close();
requestFTPUploader = null;

Download File From FTP:
string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "abc.txt";

FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDownload.Credentials = new NetworkCredential("username", "pwd");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);             
while (bytesRead > 0)
{
  writeStream.Write(buffer, 0, bytesRead);
  bytesRead = responseStream.Read(buffer, 0, Length);

responseStream.Close();
writeStream.Close();
requestFileDownload = null;
responseFileDownload = null;

Friday, March 4, 2011

Reorder the datatable

public void DoReorder(int oldIndex, int newIndex, ref DataTable DataSource, string SortOrderField)
        {
            if (DataSource is DataTable)
            {
                DataRowCollection rows = ((DataTable)DataSource).Rows;
                int NewListOrder = Convert.ToInt32(rows[newIndex][SortOrderField]);

                if (oldIndex < newIndex) //item moved down
                {
                    for (int i = oldIndex + 1; i <= newIndex; i++)
                    {
                        rows[i][SortOrderField] =
                    Convert.ToInt32(rows[i][SortOrderField]) - 1;
                    }
                }
                else  //item moved up
                {
                    for (int i = oldIndex - 1; i >= newIndex; i--)
                    {
                        rows[i][SortOrderField] =
                    Convert.ToInt32(rows[i][SortOrderField]) + 1;
                    }
                }
                rows[oldIndex][SortOrderField] = NewListOrder;

            }
            else
            {
                throw new InvalidOperationException
            ("DataSource is not a System.Data.DataTable.");
            }
        }