本文介紹了在 C# 中從 FTP 讀取文件到內(nèi)存的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
我想從 FTP 服務(wù)器讀取文件而不將其下載到本地文件.我寫了一個函數(shù),但它不起作用:
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;
}
推薦答案
這是一個簡單的工作示例,使用您的代碼從 Microsoft 公共 FTP 服務(wù)器獲取文件:
Here's a simple working example using your code to grab a file from the Microsoft 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's original code
// so for now we do nothing.
}
我認(rèn)為您在代碼中的這一行缺少斜杠:
I reckon you're missing a slash on this line here in your code:
string url = FtpPath + FileName;
可能在 FtpPath
和 FileName
之間?
Perhaps between FtpPath
and FileName
?
這篇關(guān)于在 C# 中從 FTP 讀取文件到內(nèi)存的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!