【发布时间】:2020-05-28 18:55:05
【问题描述】:
我的应用很简单:
从 ftp 获取文件列表 - 步骤 1
循环每个项目并获取文件 - 步骤 2
将内容从 xml 文件写入数据库 - 步骤 3
在移动到循环中的另一个项目之前,让我们将处理过的文件移动到 另一个文件夹 - 第 4 步(错误)
这是我的代码(请阅读 cmets!):
if (ftpFiles.Any())
{
foreach (var file in ftpFiles)
{
try
{
var item_path = $"ftp://my.url.ok:somePort/{item.FtpFolderName}/in/" + file;
FtpWebRequest xml_request = (FtpWebRequest)WebRequest.Create(item_path);
xml_request.Method = WebRequestMethods.Ftp.DownloadFile;
xml_request.Credentials = new NetworkCredential("username", "password");
using (FtpWebResponse xml_response = (FtpWebResponse)xml_request.GetResponse())
{
using (StreamReader xml_reader = new StreamReader(xml_response.GetResponseStream()))
{
// PROCESSING XML FILE AND MAKING DATABASE CALL TO INSERT VALUE TO DB AND THAT IS FINE!
}
}
// BEFORE I MOVE TO ANOTHER FILE IN LOOP I WANT TO MOVE PREVIOUS FILE TO ANOTHER FOLDER
var item_to_move_original_path = $"ftp://my.url.ok:somePort/{item.FtpFolderName}/in/" + file;
var item_to_move_new_path = $"ftp://my.url.ok:somePort/{item.FtpFolderName}/processed/" + file;
FtpWebRequest xml_request_rename = (FtpWebRequest)WebRequest.Create(item_to_move_original_path);
xml_request_rename.Method = WebRequestMethods.Ftp.Rename;
xml_request_rename.Credentials = new NetworkCredential("username", "password");
xml_request_rename.RenameTo = item_to_move_new_path;
// IN LINE BELOW APP CRASHES AND IT SAYS THAT File name is not allowed
// One more question here is can I use xml_request that I've created in a loop earlier to rename/move a file or I have to crate this
// xml_request_rename
FtpWebResponse response = (FtpWebResponse)xml_request_rename.GetResponse();
}
catch(Exception ex)
{}
}
}
【问题讨论】:
-
您能否使用现有的 FTP 客户端(如 Filezilla)将文件上传到该位置?
-
@C.Evenhuis 是的。我正在使用 FileZilla,我可以毫无问题地写入该位置