Solved

ftpwebrequest Help C#

Posted on 2016-07-28
8
26 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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 19

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 250 total points
Comment Utility
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
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mburk1968
Comment Utility
File.WriteAllText("c:\\temp\\export.csv", "reader.ReadToEnd());
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
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
Comment Utility
I cannot thank you both enough. I've been struggling with this. Awesome!
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now