Link to home
Create AccountLog in
Avatar of mburk1968
mburk1968Flag for United States of America

asked on

ftpwebrequest Help C#

I can compile this code and run it without issue in Visual Studio. However where is it saving the file that it downloads if indeed it is actually downloading a file?

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.net/site/wwwroot/ftp/Export.csv");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("Username\\$username", "password");

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

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

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

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

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

It is not saving the file at all it is displaying it to the Console with this line of code.
Console.WriteLine(reader.ReadToEnd());

Open in new window

SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mburk1968

ASKER

Receiving 3 errors

Severity      Code      Description      Project      File      Line      Suppression State
Error      CS1010      Newline in constant      FTPConsoleApplication      C:\Users\dell main\Documents\Visual Studio 2015\Projects\FTPConsoleApplication\FTPConsoleApplication\FTPConsoleApplication\Program.cs      26      Active

Severity      Code      Description      Project      File      Line      Suppression State
Error      CS1003      Syntax error, ',' expected      FTPConsoleApplication      C:\Users\dell main\Documents\Visual Studio 2015\Projects\FTPConsoleApplication\FTPConsoleApplication\FTPConsoleApplication\Program.cs      26      Active

Severity      Code      Description      Project      File      Line      Suppression State
Error      CS1026      ) expected      FTPConsoleApplication      C:\Users\dell main\Documents\Visual Studio 2015\Projects\FTPConsoleApplication\FTPConsoleApplication\FTPConsoleApplication\Program.cs      29      Active
Are these errors you are getting being returned from the reader on this line of code, Console.WriteLine(reader.ReadToEnd());? or other code?

What are on lines 26 - 30?
File.WriteAllText("c:\\temp\\export.csv", "reader.ReadToEnd());
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I cannot thank you both enough. I've been struggling with this. Awesome!
Not a problem, glad to help.