FTP - 不允许使用文件名。 (远程服务器错误 533)C#
FTP - File name not allowed. (REMOTE SERVER ERROR 533) C#
我的应用很简单:
Getting list of files from ftp - step 1
Looping each of items and getting file - step 2
Writing content from a xml file to database - step 3
Before moving to another item in loop let's move processed file to
another folder - step 4 (error)
这是我的代码(请阅读评论!):
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)
{}
}
}
我已经能够通过更改此 url 来解决此问题(删除部分 url):
var item_to_move_new_path = $"ftp://my.url.ok:somePort/{item.FtpFolderName}/processed/" + file;
至:
var item_to_move_new_path = $"/{item.FtpFolderName}/processed/" + file;
可能会注意到我删除了
ftp://my.url.ok:somePort/
从路径开始,一切正常,怎么会???
一切正常,但我不知道为什么,如果有人能向我和其他正在观看这个 Thead 的人解释一下,那就太棒了!
我的应用很简单:
Getting list of files from ftp - step 1
Looping each of items and getting file - step 2
Writing content from a xml file to database - step 3
Before moving to another item in loop let's move processed file to another folder - step 4 (error)
这是我的代码(请阅读评论!):
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)
{}
}
}
我已经能够通过更改此 url 来解决此问题(删除部分 url):
var item_to_move_new_path = $"ftp://my.url.ok:somePort/{item.FtpFolderName}/processed/" + file;
至:
var item_to_move_new_path = $"/{item.FtpFolderName}/processed/" + file;
可能会注意到我删除了
ftp://my.url.ok:somePort/
从路径开始,一切正常,怎么会???
一切正常,但我不知道为什么,如果有人能向我和其他正在观看这个 Thead 的人解释一下,那就太棒了!