gpt4 book ai didi

c# - FtpWebRequest 下载文件

转载 作者:IT王子 更新时间:2023-10-29 04:03:02 25 4
gpt4 key购买 nike

以下代码旨在通过 FTP 检索文件。但是,我遇到了错误。

serverPath = "ftp://x.x.x.x/tmp/myfile.txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);

request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;

request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);

// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}

错误是:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access)

该文件确实存在于远程机器上,我可以手动执行此 ftp(即我有权限)。谁能告诉我为什么会出现此错误?

最佳答案

我知道这是一篇旧帖子,但我在这里添加以供将来引用。这是我找到的解决方案:

    private void DownloadFileFTP()
{
string inputfilepath = @"C:\Temp\FileName.exe";
string ftphost = "xxx.xx.x.xxx";
string ftpfilepath = "/Updater/Dir1/FileName.exe";

string ftpfullpath = "ftp://" + ftphost + ftpfilepath;

using (WebClient request = new WebClient())
{
request.Credentials = new NetworkCredential("UserName", "P@55w0rd");
byte[] fileData = request.DownloadData(ftpfullpath);

using (FileStream file = File.Create(inputfilepath))
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
MessageBox.Show("Download Complete");
}
}

根据 Ilya Kogan 的出色建议更新

关于c# - FtpWebRequest 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2781654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com