1

我正在从这样的 ftp 文件夹中获取文件。

request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ipaddress/foldername"));
                request.UseBinary = true;
                request.Credentials = new NetworkCredential("username", "password");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.UseBinary = true;

                WebResponse response = request.GetResponse();

                StreamReader reader = new StreamReader(response.GetResponseStream());

                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    // to remove the trailing '\n'
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    reader.Close();
                    response.Close();

                    string[] results = result.ToString().Split('\n');

                    foreach (string filename in results)

                    {
 connetionString = "Data Source=testservername;Initial Catalog=testdb;User ID=sa;Password=sa";
                   connection = new SqlConnection(connetionString);


                   FtpWebRequest request1 = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://servername/"  + filename));
                   request1.Method = WebRequestMethods.Ftp.DownloadFile;
                   request1.UseBinary = true;
                   request1.Credentials = new NetworkCredential("username", "password");
                   responses = (FtpWebResponse)request1.GetResponse();
                   StreamReader reader1 = new StreamReader(responses.GetResponseStream());

                   string xml = reader1.ReadToEnd();



                   ds.ReadXml(new XmlTextReader(new StringReader(xml)));

                   int i = 0;
                   connection.Open();
                   for (i = 0; i <= ds.Tables[1].Rows.Count - 1 ; i++)
                    {

                   sql = "insert into Details values('" + ds.Tables[1].Rows[i].ItemArray[0]            
                                                       + "')";

                        command = new SqlCommand(sql, connection);
                        adpter.InsertCommand = command;
                        adpter.InsertCommand.ExecuteNonQuery();

                    }


                        FtpWebRequest requestDelete = (FtpWebRequest)WebRequest.Create("ftp://sername/" + filename);
                        requestDelete.Method = WebRequestMethods.Ftp.DeleteFile;
                        requestDelete.UseBinary = true;
                        requestDelete.Credentials = new NetworkCredential("username", "password");
                        responseDelete = (FtpWebResponse)requestDelete.GetResponse();
                        try
                        {
                            string res = responseDelete.StatusDescription;
                            if (res == "250 Delete operation successful.")
                            {
                                MessageBox.Show("Done......");

                            }
                        }
                        catch (Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);

                        }
                    }

                    responseDelete.Close();
                    connection.Close();
                    responses.Close();



            }
            catch (Exception ex)
            {
               System.Windows.Forms.MessageBox.Show(ex.Message);

            }
        }

您好需要从 ftp 文件夹中获取文件并一一读取该 xml 文件并将其保存为我的数据库中的记录,之后需要删除该文件 ftp 文件夹。

如果为了测试,我在 ftp 文件夹中放置了两个文件,第一个文件读取成功。但是在读取第二个文件时出现错误,例如下一行中的“对象引用未设置对象实例”。

ds.ReadXml(new XmlTextReader(new StringReader(xml)));

请告诉我我的代码有什么问题。我多次发送 ftp 请求很好,或者通过查看上面的代码行建议我如何做到这一点?

4

0 回答 0