我想列出我的ftp文件夹中的所有文件,我正在使用此代码。但它给了我两倍的文件名。这有什么问题?
private void ListFilesOnServer()
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings.Get("IncomingFtpPath"));
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (System.IO.Path.GetExtension(line) == ".xml")
{
WaitingListBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(line));
}
}
reader.Close();
response.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
答案 0 :(得分:0)
对您的代码的评论很少:
避免吞咽异常(即使您显示异常)
private void ListFilesOnServer()
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings.Get("IncomingFtpPath"));
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using(StreamReader reader = new StreamReader(response.GetResponseStream())
{
string line = null;
while((line = reader.ReadLine()) != null)
{
if (System.IO.Path.GetExtension(line) == ".xml")
{
WaitingListBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(line));
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
// throw e
}