Solved

ftpwebrequest Help C#

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

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

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 62

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

22 Experts available now in Live!

Get 1:1 Help Now