I'm very new to C#, I've done some Batch files and some stuff for a game called Arma 3 which uses SQF and C++. So please forgive my ignorance, I'm trying to learn.
I've recently written this code using a GUI rather than a console app. for educational purposes. it downloads form my FTP server and list the directory contents. However, When I Download the file, it says it downloads and it doesn't show up anywhere on my PC. The FTP server connects and even says it transferred properly.
How do I get a "Select Directory" option to show up? or even a default path?
I've tried a few things and have gotten hung up.
using System.IO;
& using System.Windows;
creates an error of ""Path"
is ambiguous"
using System.Windows;
using System.Net;
using System.IO;
namespace Downloader
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DLBTN_Click(object sender, RoutedEventArgs e)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/test.txt");
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("test", "test123");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
MessageBox.Show("Download Complete", response.StatusDescription);
}
private void CNTBTN_Click(object sender, RoutedEventArgs e)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential("test", "test123");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
MessageBox.Show(reader.ReadToEnd());
}
}
}
You're not actually doing anything with the response you get. You get the response object, but you never call response.GetResponseStream() to actually get a stream, nor do you ever open a file to write to.
You'd need something like this (just writing off the top of my head, haven't tested):
using (FileStream outStream = new FileStream(@"C:\outputfile.txt")) // or whatever
using (Stream inStream = response.GetResponseStream())
{
inStream.CopyTo(outStream); // Could also await instream.CopyToAsync() instead
}
If you want to prompt for a path to save to, you should look into the SaveFileDialog class.