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

Share This!


18 comments:

Anonymous said...

At this time it seems like Worddpress iss the best blogging platfkrm available riight now.
(from what I've read) Is that what you're
using on your blog?

Anonymous said...

Just wanna input on few general things, The website design is perfect,
the articles is really fantastic :D.

Anonymous said...

I must thank you for the efforts you've put in penning this website.
I am hoping to view the same high-grade blog posts from you later
on as well. In fact, your creative writing abilities has encouraged me to get my own site now
;)

Anonymous said...

I'm now not certain the place you are getting your information, but good
topic. I needs to spend some time studying much more
or figuring out more. Thank you for excellent information I used to be searching
for this information for my mission.

Anonymous said...

I used to be able to find good advice from your articles.

Anonymous said...

More 'n' more organizations choosing Joomla Web Design & Development in order to leverage from this amazing
open source content management system. These were imported from the
self registering COM server with all the Import button. Most web designers prefer to design and develop
their applications using dot net platform because of its interoperability feature.

Anonymous said...

Eu gosto - sempre que as pessoas reunir e compartilhar vistas .
Ótimo site , continue assim !

Anonymous said...

Eu amo que vocês são geralmente também. Tais trabalho inteligente e relatórios!

Manter-se o incrível funciona caras tenho adicionado vocês para blogroll.

Anonymous said...

Hello to every one, the contents existing at this web page are genuinely amazing for people experience, well, keep up the nice work fellows.

Anonymous said...

You really make it seem so easy with your presentation but I find this topic to be really something which
I think I would never understand. It seems tooo complex and very broad
for me. I am looking forward for your next post,
I'lltry to get the hang off it!

Anonymous said...

naturally like your web site but you need to take a look at the spelling
on several of your posts. A number of them are rife with
spelling prolems and I in finding it very bothersoime
to inform the reality on the other hand I'll certainly come again again.

Anonymous said...

Very golod information. Lucky me I recently found your site by accident (stumbleupon).

I have saved it for later!

Anonymous said...

Hey I know this is off topic but I was wondering if you knew off any widgets I could add to my blog that automatically tweet my newest twitter updates.
I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
Please let me know if youu run into anything.
I truly enjoy reading your blog and I look forward tto your new updates.

Anonymous said...

I like this website very much so much great information.

Anonymous said...

What's Happening i'm new to this, I stumbled upon this I've found It absolutely helpful and it has
helped me out loads. I hope to contribute & aid different customers like its aided me.
Good job.

Anonymous said...

Right now it appears like Movable Type is the best blogging platform available right now.
(from what I've read) Is that what you are using on your blog?

Anonymous said...

Asking questions are really fastidious thing if you are not understanding something completely, except
this post gives good understanding even.

Unknown said...

Slots by Pragmatic Play - AprCasino
Pragmatic Play. Pragmatic Play. 샌즈카지노 Pragmatic Play. 블랙 잭 Slot Machine. The Dog House. Slots. Wild 메리트 카지노 가입 쿠폰 West Gold. Pragmatic 카지노 사이트 Play. 카지노 검증 사이트 Jackpot Party.

Powered By Blogger · Designed By Seo Blogger Templates