我创建了一个FTP,我想使用C#代码读取一些数据。当 FTP 没有用户名/密码访问权限时,一切正常。但是当我输入用户名和密码时,我得到
The remote server returned an error: (530) Not logged in
。
我尝试了 Stack Overflow 和互联网上的所有问题,例如使用
.Normalize()
和使用 @username
,但我不断收到该错误。
这是我的代码:
foreach (string fileNameInFTP in directories)
{
// string fileNameInFTP2 = Path.GetFileNameWithoutExtension(fileNameInFTP);
if ((!haveWeAlreadyParsedThisFile(fileNameInFTP)) && (fileNameInFTP.Contains("CustsExport")) && (!fileNameInFTP.EndsWith("Empty.xml")) && (!fileNameInFTP.Contains("DelCustsExport")))
{
string file = FTPAddress + "/" + fileNameInFTP;
Console.WriteLine(file);
List<Customer> customersList =
(
from e in XDocument.Load(file).Root.Elements("cust")
select new Customer
{
MemeberID = (int)e.Attribute("memberid"),
CustomerID = (int)e.Attribute("custid"),
FirstName = (string)e.Attribute("fname"),
LastName = (string)e.Attribute("lname"),
ShowsNumber = (int)e.Attribute("count_noshow"),
VisitNumber = (int)e.Attribute("count_resos"),
Cancellation = (int)e.Attribute("count_cancel"),
MobileNumber = (string)e.Element("phone").Attribute("phonenumber")
/*Projects =
(
from p in e.Elements("projects").Elements("project")
select new Project
{
ProjectCode = (string)p.Element("code"),
ProjectBudget = (int)p.Element("budget")
}).ToArray()*/
}).ToList();
List<Customer> customersList =
(
from e in XDocument.Load(file).Root.Elements("cust")
select new Customer
{
MemeberID = (int)e.Attribute("memberid"),
CustomerID = (int)e.Attribute("custid"),
FirstName = (string)e.Attribute("fname"),
LastName = (string)e.Attribute("lname"),
ShowsNumber = (int)e.Attribute("count_noshow"),
VisitNumber = (int)e.Attribute("count_resos"),
Cancellation = (int)e.Attribute("count_cancel"),
MobileNumber = (string)e.Element("phone").Attribute("phonenumber")
/*Projects =
(
from p in e.Elements("projects").Elements("project")
select new Project
{
ProjectCode = (string)p.Element("code"),
ProjectBudget = (int)p.Element("budget")
}).ToArray()*/
}).ToList();
换句话说:我可以读取文件的名称,但不能读取文件的内容。
您可以在 FTP URL 上提供用户名和密码。例如,如果你想从
/path/filename
下载ftp://ftp.foo.com
,你可以像这样编码用户名和密码:
ftp://username:[email protected]/path/filename
您应该能够为您尝试下载的文件构建这样的 URL。然后将该 URL 传递给
XDocument.Load
。
但请注意,密码是以明文形式发送的(即未加密等)。
请参阅 RFC 1738 的第 3.2 节了解更多信息。