Solved

FTP Client

Posted on 2006-06-09
8
400 Views
Last Modified: 2010-04-16
How does one list the directory on an ftp server and then download the contents, is it similiar to uploading? this is the code i have for my upload method:

           DirectoryInfo hostDir = new DirectoryInfo(hostDirectory);
            FileInfo[] hostInfos = hostDir.GetFiles("*.txt");
            bool IsHostSuccess = true;

            foreach (FileInfo hostFileInfo in hostInfos)
            {
                if (null != hostFileInfo)
                {
                    IsHostSuccess = UploadFile(hostFileInfo);
                    if (!IsHostSuccess)
                    {
                        errCount = errCount + 1;
                        if (errCount > 100)
                            break;
                    }
                }
            }

I basically want to do the same exact thing except downloading and instead of listing the current dictory locally, listing the directory remotely.
0
Comment
Question by:JoeDW
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:existenz2
Comment Utility
You can find several FTP implementations on the web, which can do all these actions for you.

For example:
http://www.codeproject.com/cs/internet/ftplib.asp
http://www.codeproject.com/cs/internet/ftplibrary.asp
0
 
LVL 1

Author Comment

by:JoeDW
Comment Utility
I'd really rather not use someone elses, as I am trying to learn this on my own and would just like to see some examples...I mean the application is complete asside from listing the directory. This isnt a full FTP client, it's simply a small app that uses the FTP protocol to upload like 6 files and short of having to hard code those file names in there I would like to just list the directory for *.txt files, if possible without using an entire library.

Thanks
Joe
0
 
LVL 14

Expert Comment

by:existenz2
Comment Utility
In that case I suggest you to read the FTP RFC: http://www.faqs.org/rfcs/rfc959.html and the given urls as examples.
0
 
LVL 1

Author Comment

by:JoeDW
Comment Utility
Well if I wasn't strapped for time and had a few days to read over it that would be fine, but then I wouldn't have posted my question to Experts Exchange would I? I am not trying to be rude but the entire reason for posting here is usually to find answers, fast. I am sure that is a very good read but for what I am trying to accomplish its overkill. I have since figured it out and simply used the Ftp ListDirectory and Regular Expressions to match for the files I am looking for

public string ListDirectory()
        {
            string files = null;
            NetworkCredential credentials = new NetworkCredential(username, password);
            string serverAddress = "ftp://" + server + "/";
            FtpWebRequest request = null;
            FtpWebResponse response = null;

            request = (FtpWebRequest)WebRequest.Create(new Uri(serverAddress));

            request.Method = WebRequestMethods.Ftp.ListDirectory;

            request.Credentials = credentials;
            response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            string [] fileArray = new string[] { reader.ReadToEnd() };

            foreach (string file in fileArray)
            {
                files = file;
            }

            reader.Close();
            response.Close();
         
            return files;
         
  }

Thanks anyways
Joe
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 250 total points
Comment Utility
Uploading files is more complex then downloading. You need to initiate quite some stuff. The given samples show you exactly how it can be done. Another example: http://www.csharphelp.com/archives/archive219.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now