Solved

ftpwebrequest Help C#

Posted on 2016-07-28
8
53 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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