FTP The remote server returned an error: (530) Not

2019-08-11 14:44发布

I have been trying to solve this problem more than 4 hours, it is getting me crazy.

I created an FTP, and I wanna read some data using c# code.

when the FTP has no username/password access, everything works perfectly. but when I put username and password, i got this error message: The remote server returned an error: (530) Not logged in.

I tried all the questions on stackoverflow and internet like: using .Normalize(), and using @username, but I keep getting that error.

this is my code:

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();

:please note

that I am able to access the FTP because the directories variable is the list of the files in the FTP, and when I debug the code, I can see that it has the files, but the exception accurs in this line:

                    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();

in other words: I am able to read the names of the files, but not the content of them

标签: c# ftp
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-11 15:30

You can supply the user name and password on the FTP URL. For example, if you want to download /path/filename from ftp://ftp.foo.com, you can encode the user name and password like this:

ftp://username:password@ftp.foo.com/path/filename

You should be able to construct such a URL for the file you're trying to download. Then pass that URL to XDocument.Load.

Note, however, that the password is sent in the clear (i.e. no encryption, etc.).

See Section 3.2 of RFC 1738 for more info.

查看更多
登录 后发表回答