使用C# 的Ftp功能时,发现了一个很奇怪的现象,获取目录列表的命令,在SeverU上面直接返回错误,而在windows自带的FTP上则正常,经过反复试验,终于发现,原来是ServerU默认情况下,不允许使用NLST命令获取目录列表,只要在 “设置”-〉“高级”中,选中“NLST命令也列出目录”即可。
下面是测试代码:
下面是测试代码:
1 try
2 {
3 //检查一下日期目录是否存在
4 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(directory);
5
6 reqFTP.UseBinary = true;
7
8 reqFTP.Credentials = new NetworkCredential(GlobalManager.FTPUserID, GlobalManager.FTPPassword);
9
10 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
11
12 Stream stream = reqFTP.GetResponse().GetResponseStream();
13
14 using (StreamReader sr = new StreamReader(stream))
15 {
16 string line = sr.ReadLine();
17
18 while (!string.IsNullOrEmpty(line))
19 {
20 if (line == foldName)
21 {
22 exist = true;
23 break;
24 }
25
26 line = sr.ReadLine();
27 }
28 }
29 }
30 catch (Exception e)
31 {
32 exist = false;
33 }
2 {
3 //检查一下日期目录是否存在
4 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(directory);
5
6 reqFTP.UseBinary = true;
7
8 reqFTP.Credentials = new NetworkCredential(GlobalManager.FTPUserID, GlobalManager.FTPPassword);
9
10 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
11
12 Stream stream = reqFTP.GetResponse().GetResponseStream();
13
14 using (StreamReader sr = new StreamReader(stream))
15 {
16 string line = sr.ReadLine();
17
18 while (!string.IsNullOrEmpty(line))
19 {
20 if (line == foldName)
21 {
22 exist = true;
23 break;
24 }
25
26 line = sr.ReadLine();
27 }
28 }
29 }
30 catch (Exception e)
31 {
32 exist = false;
33 }