I want to read a file from a FTP server without downloading 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;
}
The code you have above is very simillar to another Stackoverlow example I found and used myself 2 days ago. Provided you set the URI, User and Password correctly, it will download the file and set the contents to FileString. I'm not sure what you mean by wanting to read the file without downloading it. This is not really an option.
If you want to look at file attributes (I see you mention "version"), you could use the code below to get all the file Name, Data, and Size from the FTP server without downloading the file.
Call GetFileInfo and pass in the file name (make sure you follow through the code to set the full FTP path, User and Password). This will give you back a FTPFileInfo object with a Name, Date and Size.
It is impossible to know what the issue is without details about the error/exception.
At a guess, you do not seem to be assigning a new value to
version
after the initial declarationTry changing your code to
C Sharp GUI app. Minimal ftp transfer, not elegant but it works fine. GUI, not console.
Weather from noaa. Find your region (look for your metar)! A METAR weather report is predominantly used by pilots in fulfillment of a part of a pre- flight
Build with vs 2012 premium RC (july 2012)
(click on label, not button)
Small binary file
If the file is small, the easiest way is using
WebClient.DownloadData
:Small text file
If the small file is a text file, use
WebClient.DownloadString
:It assumes that the file contents is in UTF-8 encoding (a plain ASCII file will do too). If you need to use a different encoding, use
WebClient.Encoding
property.Large binary file
If the file is large, so that you need to process it in chunks, instead of loading it to memory whole, use
FtpWebRequest
:Large text file
If the large file is a text file and you want to process it by lines, instead of by chunks of a fixed size, use
StreamReader
:Again, this assumes UTF-8 encoding. If you want to use another encoding, use an overload of
StreamReader
constructor that takes alsoEncoding
.Take a look at my FTP class, it might be exactly what you need.
To initialize:
https://stackoverflow.com/a/28669746/2701974