FTP directory browsing and file download

Hi,
I am having a problem when I log in to the ftp server to download a file.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://nndata.se/user/test.txt");

request.Method = WebRequestMethods.Ftp.DownloadFile;

request.Credentials = new NetworkCredential("user", "pw");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

I want to download the test file from that specific directory above. However when I use this code in some way the ftp server adds the "user" string to the directory and starts looking for the test.txt file in this directory:

nndata.se/user/user/test.txt

I get a
a file not found exception back.
Anyone knows how to solve this, without changing the directories on the server, only by changing the code



Answer this question

FTP directory browsing and file download

  • dgracey

    Posting in a new thread is much better - this way we can track the issues easily and know which one is already resolved and which isn't, plus it is easier for other customers to search by topic. So if you have other questions, please do open a new thread.

    There is no direct save, you can use StreamReader.ReadToEnd and then directly writethis to a file using StreamWriter. Here is what you can use: I've added an additional string to make it easier for you to see what's going on

    StreamReader reader = null;

    StreamWriter writer = null;

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);

    request.Method = WebRequestMethods.Ftp.DownloadFile;

    request.Credentials = new NetworkCredential("anonymous", "");

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

    Stream respStream = response.GetResponseStream();

    reader = new StreamReader(respStream);

    string streamContent = reader.ReadToEnd();

    Console.WriteLine(streamContent);

    writer = new StreamWriter("FileName.txt", true);

    writer.Write(streamContent);

    writer.Close();

     

    Mariya


  • AlexDcosta

    Thank you for the answer. I did not realise that "user" directory is the root.


  • Soteriologist

    I am guessing "user" is your ftp root, right The uri has to be from the root directory up.
    Try connecting with the following string "ftp://nndata.se/test.txt"

    Mariya


  • Dino Nguyen

    Maybe I should post this in a new thread, but now when I can access the FTP site new brainteasers emerge. What I want is to download the file, but I have not found a class for downloading the file "as is". I first have to open a read stream and read from the ftp, then I have to open a write stream to save the contents on my computer. It looks like this (continuing the code in my previous post):

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


    string ss;
    using (StreamWriter sw = File.CreateText(path))
    {
        while ((ss = reader.ReadLine() )!= null)
        {
            sw.WriteLine(ss);
            Console.WriteLine(ss);
        }
    }

    This code works until I reach EOF. Then the while clause hangs and says I am using a disposed object, the reader.

    So in short, two questions:
    1) Is there a class for "direct-save" of the ftp file
    2) How do I avoid to dispose the reader


  • FTP directory browsing and file download