Solved

ftpwebrequest Help C#

Posted on 2016-07-28
8
36 Views
Last Modified: 2016-07-28
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

0
Comment
Question by:mburk1968
  • 4
  • 3
8 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41733542
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

0
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 250 total points
ID: 41733552
You can save it using File.WriteAllText.

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());

           // Will save it to c:\temp\export.csv
           File.WriteAllText("c:\\temp\\export.csv", "reader.ReadToEnd());

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

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

Open in new window

0
 

Author Comment

by:mburk1968
ID: 41733656
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
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41733667
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?
0
 

Author Comment

by:mburk1968
ID: 41733678
File.WriteAllText("c:\\temp\\export.csv", "reader.ReadToEnd());
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 41733684
That line of code should be as follows.
File.WriteAllText("c:\\temp\\export.csv", reader.ReadToEnd()); 

Open in new window

With this line of code the file will be located on the C: drive in a folder temp and the file name export.csv
0
 

Author Closing Comment

by:mburk1968
ID: 41733737
I cannot thank you both enough. I've been struggling with this. Awesome!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41733741
Not a problem, glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.

832 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