Friday, December 10, 2010

How to get list of file and directory names from remote direcotry

Many of times we requires to get list of all name of files and folder which exists on remote server.

To get list of File and Folder names from Remote Directory / Remote Server

public void getDirList()
        {
            FtpWebRequest _lsDirFtp;

            _lsDirFtp = (FtpWebRequest)FtpWebRequest.Create("ftp://myFTPIPorName//MyDirecoty/");
            _lsDirFtp.KeepAlive = false;
            _lsDirFtp.Credentials = new NetworkCredential("username", "password");

            _lsDirFtp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;



            WebResponse response = _lsDirFtp.GetResponse();
            
            StreamReader reader = new StreamReader(response.GetResponseStream());



            //Get Directory/File names

            FtpWebRequest _lsDirDetailsFtp;
            _lsDirDetailsFtp = (FtpWebRequest)FtpWebRequest.Create("ftp://myFTPIPorName//MyDirecoty/");
            _lsDirDetailsFtp.KeepAlive = false;

            _lsDirDetailsFtp.Credentials = new NetworkCredential("username", "password");

            _lsDirDetailsFtp.Method = WebRequestMethods.Ftp.ListDirectory;



            WebResponse response2 = _lsDirDetailsFtp.GetResponse();

            StreamReader reader2 = new StreamReader(response2.GetResponseStream());



            //read file/directory names into arraylist

            string lsdirectory = reader2.ReadLine();

            ArrayList lsnames = new ArrayList();

            while (lsdirectory != null)
            {

                lsnames.Add(lsdirectory);
                Response.Write("fileName:" + lsdirectory);

                lsdirectory = reader2.ReadLine();

            }



            //read through directory details response

            string line = reader.ReadLine();

            while (line != null)
            {

                //if (line.StartsWith("d") && !line.EndsWith(".")) //"d" = dir don't need "." or ".." dirs
                //{

                    foreach (String chk in lsnames) //compare basic dir output to detail dir output to get dir name
                    {

                        if (line.EndsWith(chk))
                        {

                            //found dir
                          Response.Write ("fileName:" + chk);

                            // Console.WriteLine(chk);

                        }

                    }

               // }

                line = reader.ReadLine();

            }


        }

Solution By:

Rajesh Rolen

Read More

The remote server returned an error: (503) Bad sequence of commands.

While working with FTP some times you gets this error "The remote server returned an error: (503) Bad sequence of commands." to resolve it just do set

_request.KeepAlive = false;

actually by default it keeps your ftp request connection open even your work is done.but when we will set its KeepAlive to false so now it will not give you the error again.

Solution By:

Rajesh Rolen

Read More

Thursday, December 9, 2010

How to upload / download / delete file using FTP in asp.net / vb.net

We can easily upload or download files from FTP using asp.net/vb.net:


To Upload file to FTP/Remote Server using VB.NET

Protected Sub btnUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myFtpWebRequest As FtpWebRequest
Dim myFtpWebResponse As FtpWebResponse
Dim myStreamWriter As StreamWriter

myFtpWebRequest = WebRequest.Create("ftp://ftp_server_name/filename.ext")

'myFtpWebRequest.Credentials = New NetworkCredential("username", "password")

myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
myFtpWebRequest.UseBinary = True

myStreamWriter = New StreamWriter(myFtpWebRequest.GetRequestStream())
myStreamWriter.Write(New StreamReader(Server.MapPath("filename.ext")).ReadToEnd)
myStreamWriter.Close()

myFtpWebResponse = myFtpWebRequest.GetResponse()

litResponse.Text = myFtpWebResponse.StatusDescription

myFtpWebResponse.Close()
End Sub


To Download file from FTP/Remote Server using VB.NET

Protected Sub btnDownloadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myFtpWebRequest As FtpWebRequest
Dim myFtpWebResponse As FtpWebResponse
Dim myStreamWriter As StreamWriter

myFtpWebRequest = WebRequest.Create("ftp://ftp_server_name/filename.ext")

'myFtpWebRequest.Credentials = New NetworkCredential("username", "password")

myFtpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile
myFtpWebRequest.UseBinary = True

myFtpWebResponse = myFtpWebRequest.GetResponse()

myStreamWriter = New StreamWriter(Server.MapPath("filename.ext"))
myStreamWriter.Write(New StreamReader(myFtpWebResponse.GetResponseStream()).ReadToEnd)
myStreamWriter.Close()

litResponse.Text = myFtpWebResponse.StatusDescription

myFtpWebResponse.Close()
End Sub

To Delete file from FTP/Remote Server using VB.NET

Protected Sub btnDeleteFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myFtpWebRequest As FtpWebRequest
Dim myFtpWebResponse As FtpWebResponse

myFtpWebRequest = WebRequest.Create("ftp://ftp_server_name/filename.ext")

'myFtpWebRequest.Credentials = New NetworkCredential("username", "password")

myFtpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile

myFtpWebResponse = myFtpWebRequest.GetResponse()

litResponse.Text = myFtpWebResponse.StatusDescription

myFtpWebResponse.Close()
End Sub
Read More

Wednesday, December 8, 2010

How to show immediate window.

Some times in visual studio the immediate window gets disappeared.
to get it just press: ctrl+alt+I and its done..

Solution by

Rajesh Rolen

Read More

Friday, December 3, 2010

How to set PopUp Window size of current Page

To change height and width of current window we can use resizeTo function like:
window.resizeTo(iWidth, iHeight) 


Solution By

Rajesh Rolen

Read More

Thursday, December 2, 2010

Send file on FTP using .net

To send a file over FTP using asp.net / c#.net / vb.net :

using System.Net;

public void ftpfile(string ftpfilepath, string inputfilepath)
    {

        string ftphost = "122.22.1.1"; // destination IP address      

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;

        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("userid", "password");
        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        FileStream fs = File.OpenRead(inputfilepath);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
        Stream ftpstream = ftp.GetRequestStream();
        ftpstream.Write(buffer, 0, buffer.Length);
        ftpstream.Close();

    }  

Solution by: Rajesh Rolen

Read More
Powered By Blogger · Designed By Seo Blogger Templates