?
Solved

FTP Client

Posted on 2006-06-09
8
Medium Priority
?
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 16870540
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
ID: 16870590
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
ID: 16871985
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
ID: 16872082
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 1000 total points
ID: 16872189
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 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