ftp服务器文件名存在,有什么办法根据已知文件名来检测FTP服务器上是否存在该文件? 急...
蠱毒傳說(shuō)
我不敢說(shuō)
_________________
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;namespace test{ class Program { static void Main(string[] args) { string path = "ftp://192.168.1.200/services/"; string[] file = GetFileList(path); Console.ReadLine(); } public static string[] GetFileList(string path)//上面的代碼示例了如何從ftp服務(wù)器上獲得文件列表 { string[] downloadFiles; FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); ftp.UseBinary = true; //ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//如果是匿名登錄,則注釋掉。 StringBuilder result = new StringBuilder(); try { ftp.Method = WebRequestMethods.Ftp.ListDirectory; WebResponse response = ftp.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名 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(); downloadFiles = result.ToString().Split('\n'); } catch (Exception ex) { throw ex; } return downloadFiles; } }}
總結(jié)
以上是生活随笔為你收集整理的ftp服务器文件名存在,有什么办法根据已知文件名来检测FTP服务器上是否存在该文件? 急...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 安卓调试_【转】Android
- 下一篇: oracle 几个字段中某个字段大于0其