gpt4 book ai didi

c# - 在c#中使用ftp下载文件

转载 作者:行者123 更新时间:2023-11-30 14:22:08 26 4
gpt4 key购买 nike

<分区>

作为初级开发人员,我应该找到一个使用 ftp 下载文件的解决方案,我有这段代码。
它可以工作,但有时我无法打开下载的文件。

public static bool DownloadDocument(string ftpPath, string downloadPath) {
bool retVal = false;
try {
Uri serverUri = new Uri(ftpPath);
if (serverUri.Scheme != Uri.UriSchemeFtp) {
return false;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;

using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
using (Stream responseStream = response.GetResponseStream()) {
using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create)) {
int Length = 1024 * 1024 * 30;
Byte[] buffer = new Byte[Length];
responseStream.Read(buffer, 0, Length);
}
}
}
retVal = true;
}
catch (Exception ex) {
//Error logging to add
}

return retVal;
}

请有任何想法!

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