Reading file from FTP to memory in C #

I want to read a file from an FTP server without uploading it to a local file. I wrote a function, but it does not work:

private string GetServerVersion() { WebClient request = new WebClient(); string url = FtpPath + FileName; string version = ""; request.Credentials = new NetworkCredential(ftp_user, ftp_pas); try { byte[] newFileData = request.DownloadData(new Uri(FtpPath)+FileName); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); } catch (WebException e) { } return version; } 
+8
c # ftp webclient
source share
9 answers

Here is a simple working example using your code to capture a file from Microsoft's public FTP servers:

 WebClient request = new WebClient(); string url = "ftp://ftp.microsoft.com/developr/fortran/" +"README.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); try { byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); Console.WriteLine(fileString); } catch (WebException e) { // Do something such as log error, but this is based on OP original code // so for now we do nothing. } 

I believe your code is missing a slash in this line:

 string url = FtpPath + FileName; 

Perhaps between FtpPath and FileName ?

+23
source share

The code you provided above is very similar to another Stackoverlow example that I found and used myself 2 days ago. If you configured the URI, User, and Password correctly, it will download the file and set it to FileString. I'm not sure what you mean by wanting to read the file without downloading it. This is not an option.

If you want to look at the file attributes (I see that you are mentioning the β€œversion”), you can use the code below to get all the name, data and file size from an FTP server without downloading the file.

Call GetFileInfo and pass the file name (make sure that you execute the code to set the full FTP path, User and Password). This will give you an FTPFileInfo object with name, date and size.

  public static FTPFileInfo GetFileInfo(string fileName) { var dirInfo = WordstockExport.GetFTPDirectoryDetails(); var list = FTPFileInfo.Load(dirInfo); try { FTPFileInfo ftpFile = list.SingleOrDefault(f => f.FileName == fileName); return ftpFile; } catch { } return null; } class FTPFileInfo { private DateTime _FileDate; private long _FileSize; private string _FileName; public DateTime FileDate { get { return _FileDate; } set { _FileDate = value; } } public long FileSize { get { return _FileSize; } set { _FileSize = value; } } public string FileName { get { return _FileName; } set { _FileName = value; } } public FTPFileInfo() { } public static FTPFileInfo LoadFromLine(string line) { FTPFileInfo file = new FTPFileInfo(); string[] ftpFileInfo = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); file._FileDate = DateTime.Parse(ftpFileInfo[0] + " " + ftpFileInfo[1]); file._FileSize = long.Parse(ftpFileInfo[2]); file._FileName = ftpFileInfo[3]; return file; } public static List<FTPFileInfo> Load(string listDirectoryDetails) { List<FTPFileInfo> files = new List<FTPFileInfo>(); string[] lines = listDirectoryDetails.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach(var line in lines) files.Add(LoadFromLine(line)); return files; } } private static string GetFTPDirectoryDetails() { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(App.Export_FTPPath); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; request.Credentials = new NetworkCredential(App.FTP_User, App.FTP_Password); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string fileLines = reader.ReadToEnd(); reader.Close(); response.Close(); return fileLines; } 
+2
source share

It is not possible to find out what the problem is without any details about the error / exception.

Assuming you are not assigned a new version value after the initial declaration

 string version = ""; 

Try changing your code to

 version = System.Text.Encoding.UTF8.GetString(newFileData); 
0
source share

C Sharp GUI app. Minimal ftp transfer, not elegant, but it works great. GUI, not a console.

Weather from noah. Find your region (look for your metar)! METAR weather report is mainly used by pilots for part of preflight

Build with vs 2012 premium RC (July 2012)

(click on the label, not the button)

 using System; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; using System.Collections.Generic; namespace getweather { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } private void CYYY_Click(object sender, EventArgs e) { WebClient request = new WebClient(); string url = "ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/CYYY.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); richTextBox1.Text = fileString; } private void CYSC_Click(object sender, EventArgs e) { WebClient request = new WebClient(); string url = "ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/CYSC.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); richTextBox1.Text = fileString; } private void CYQB_Click(object sender, EventArgs e) { WebClient request = new WebClient(); string url = "ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/CYQB.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); richTextBox1.Text = fileString; } private void CYUY_Click(object sender, EventArgs e) { WebClient request = new WebClient(); string url = "ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/CYUY.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); richTextBox1.Text = fileString; } private void CYHU_Click(object sender, EventArgs e) { WebClient request = new WebClient(); string url = "ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/CYHU.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); richTextBox1.Text = fileString; } } } 
0
source share
 WebClient request = new WebClient(); string url = "ftp://ftp.microsoft.com/developr/fortran/" +"README.TXT"; request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com"); request.Proxy = null; try { byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); Console.WriteLine(fileString); } catch (WebException e) { // Do something such as log error, but this is based on OP original code // so for now we do nothing. } 
0
source share

I know this is an old question, but I thought I'd suggest using path.combine for the next guy reading this. Helps fix these issues.

  string url = Path.Combine("ftp://ftp.microsoft.com/developr/fortran", "README.TXT"); 
0
source share

If the file is small, the easiest way is WebClient.DownloadData :

 WebClient client = new WebClient(); string url = "ftp://ftp.example.com/remote/path/file.zip"; client.Credentials = new NetworkCredential("username", "password"); byte[] contents = client.DownloadData(url); 

If the file is a text file, use WebClient.DownloadString :

 string contents = client.DownloadString(url); 

The contents of the file are assumed to be UTF-8 encoded (a simple ASCII file will also be used). If you need to use a different encoding, use the WebClient.Encoding property .


If the file is large, so you need to process it in chunks, instead of loading it into the whole memory, use FtpWebRequest :

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.DownloadFile; using (Stream stream = request.GetResponse().GetResponseStream()) { byte[] buffer = new byte[10240]; int read; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { // process the chunk in "buffer" } } 

If the large file is a text file and you want to process it line by line rather than pieces of a fixed size, use StreamReader :

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.txt"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.DownloadFile; using (Stream stream = request.GetResponse().GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); // process the line Console.WriteLine(line); } } 

Again, this assumes UTF-8 encoding. If you want to use a different encoding, use the StreamReader contructor overload, which also accepts Encoding .

0
source share

We can use the method below to get the file attribute from ftp without downloading the file. This works great for me.

  public DataTable getFileListFTP(string serverIP,string userID,string Password) { DataTable dt = new DataTable(); string[] fileListArr; string fileName = string.Empty; long fileSize = 0; // DateTime creationDate; string creationDate; FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(serverIP); Request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; Request.Credentials = new NetworkCredential(userID,Password); FtpWebResponse Response = (FtpWebResponse)Request.GetResponse(); Stream ResponseStream = Response.GetResponseStream(); StreamReader Reader = new StreamReader(ResponseStream); dt.Columns.Add("FileName", typeof(String)); dt.Columns.Add("FileSize", typeof(String)); dt.Columns.Add("CreationDate", typeof(DateTime)); //CultureInfo c = new CultureInfo("ES-ES"); while (!Reader.EndOfStream)//Read file name { fileListArr = Convert.ToString(Reader.ReadLine()).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); fileSize = long.Parse(fileListArr[4]); creationDate = fileListArr[6] + " " + fileListArr[5] + " " + fileListArr[7]; //creationDate =Convert.ToDateTime(fileListArr[6] + " " + fileListArr[5] + " " + fileListArr[7], c).ToString("dd/MMM/yyyy", DateTimeFormatInfo.InvariantInfo); fileName = Convert.ToString(fileListArr[8]); DataRow drow = dt.NewRow(); drow["FileName"] = fileName; drow["FileSize"] = fileName; drow["CreationDate"] = creationDate; dt.Rows.Add(drow); } Response.Close(); ResponseStream.Close(); Reader.Close(); return dt; } 
-one
source share

Take a look at my FTP class, this may be exactly what you need.

 Public Class FTP '-------------------------[BroCode]-------------------------- '----------------------------FTP----------------------------- Private _credentials As System.Net.NetworkCredential Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String) setCredentials(_FTPUser, _FTPPass) End Sub Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String) Dim _FileInfo As New System.IO.FileInfo(_FileName) Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest) _FtpWebRequest.Credentials = _credentials _FtpWebRequest.KeepAlive = False _FtpWebRequest.Timeout = 20000 _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile _FtpWebRequest.UseBinary = True _FtpWebRequest.ContentLength = _FileInfo.Length Dim buffLength As Integer = 2048 Dim buff(buffLength - 1) As Byte Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead() Try Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream() Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength) Do While contentLen <> 0 _Stream.Write(buff, 0, contentLen) contentLen = _FileStream.Read(buff, 0, buffLength) Loop _Stream.Close() _Stream.Dispose() _FileStream.Close() _FileStream.Dispose() Catch ex As Exception MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String) Try Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath) _request.KeepAlive = False _request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile _request.Credentials = _credentials Dim _response As System.Net.FtpWebResponse = _request.GetResponse() Dim responseStream As System.IO.Stream = _response.GetResponseStream() Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create) responseStream.CopyTo(fs) responseStream.Close() _response.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String) Dim ret As New List(Of String) Try Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath) _request.KeepAlive = False _request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails _request.Credentials = _credentials Dim _response As System.Net.FtpWebResponse = _request.GetResponse() Dim responseStream As System.IO.Stream = _response.GetResponseStream() Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream) Dim FileData As String = _reader.ReadToEnd Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) For Each l As String In Lines ret.Add(l) Next _reader.Close() _response.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try Return ret End Function Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String) _credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass) End Sub End Class 

To initialize:

 Dim ftp As New FORM.FTP("username", "password") ftp.UploadFile("c:\file.jpeg", "ftp://domain/file.jpeg") ftp.DownloadFile("c:\file.jpeg", "ftp://ftp://domain/file.jpeg") Dim directory As List(Of String) = ftp.GetDirectory("ftp://ftp.domain.net/") ListBox1.Items.Clear() For Each item As String In directory ListBox1.Items.Add(item) Next 

stack overflow

-3
source share

All Articles