远程目录和文件判断
C# winForm 获取虚拟目录中的文件信息
查到这种方法 System.DirectoryServices.DirectoryEntry Entry = new DirectoryEntry( "IIS://IP地址/W3SVC/1/Root "); foreach(DirectoryEntry de in Entry.Children) { if(de.SchemaClassName == "IIsWebVirtualDir ") { if(de.Children != null) { node = new TreeNode(de.Name); tree.Nodes.Add(node); string path = (string)de.Properties[ "Path "].Value ; Dinfo = new DirectoryInfo(path); EnumChildren(Dinfo,node); } } } 但是就是不知道 System.DirectoryServices.DirectoryEntry Entry = new DirectoryEntry( "IIS://IP地址/W3SVC/1/Root "); 怎么写?
C# 获取Ftp某个目录下的所有文件(不要文件夹)
public List<string> GetFileList(string srcpath) { List<string> list = new List<string>(); FtpWebRequest reqFtp; WebResponse response = null; StreamReader reader = null; string uri = string.Format("ftp://{0}/{1}",ftpServerIP,srcpath); try { reqFtp = (FtpWebRequest)FtpWebRequest.Create(uri); reqFtp.UseBinary = true; reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFtp.Method = WebRequestMethods.Ftp.ListDirectory; response = reqFtp.GetResponse(); reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string line = reader.ReadLine(); while (line != null) { if (!string.IsNullOrEmpty(line.Trim())) { list.Add(line.Trim()); } line = reader.ReadLine(); } reader.Close(); response.Close(); } catch { if (reader != null) { reader.Close(); } if (response != null) { response.Close(); } } return list; } 使用.NET类库中的类,先获取文件列表,然后再下载 上面的方法有个问题,会将目录下面的目录也列进来,然后下载的时候会在下载目录里面生成一个与目录名同名的并且占0字节的文件 比如 a.txt ----- 文件 xxx字节 ------文件夹 0字节 c -------文件 xxx字节 d --------文件 0字节 那么得到的文件列表就是 a.txt b c d,下载后下载目录中有文件 a.txt ----- 文件 xxx字节 ------文件 0字节 c -------文件 xxx字节 d --------文件 0字节 现在的问题是,我只需要列出文件,不需要文件夹,该用什么方式来呢 (1) 使用扩展名是不可行的,虽然在windows一般文件都是带有扩展名的,但是也会存在无扩展名的文件,如上面列出的c,d都是文件,一样没有扩展名 (2)使用文件大小来判断也是不可行的,文件夹占用空间为0,如果将0字节大小的不列入文件列表,但是如 d,本身是占用0字节的 我来帮他解答 2011-11-23 16:53 满意回答 我在之前做过一个FTP的客户端工具。 drw 文件夹 -rw 文件(有扩展名或无扩展名) 我是根据服务端返回的报文进行分析获取的列表。 给你一些代码片段: /// <summary> /// 获取指定目录下的文件和文件夹。 /// </summary> /// <param name="path">要获取的目录</param> /// <param name="WRMethods">要发送到FTP服务器的密令。</param> /// <returns></returns> public string[] GetFileList(string path, string WRMethods)//从ftp服务器上获得文件列表 { WebResponse response; string[] downloadFiles; int conut = 4; StringBuilder result = new StringBuilder(); Connect(path); if (FTPVariable.IsUseProxy_ftp) { reqFTP.Proxy = FtpProxy.GetFtpSelectProxy(FTPVariable.FtpCommand_transferProxyName); } reqFTP.ReadWriteTimeout = 12000; //如果不应销毁到服务器的连接,则为 true;否则为 false。默认值为 true。 // reqFTP.Method = WRMethods; try { response = (FtpWebResponse)reqFTP.GetResponse(); goto Ftp_lbl_03; } catch (WebException webex) { GetReply(webex.Message); if (ReplyCode == 530)// 未登录。 { goto Ftp_lbl_04; } else if (ReplyCode == 550) { goto Ftp_lbl_04; } else { FtpManage.SetLog("获取列表超时,等候1秒后重试!"); goto Ftp_lbl_01; } } Ftp_lbl_01: try { FtpManage.SetLog("正在连接服务器 " + FtpRemoteHost); response = GetRequest(path, WRMethods); } catch (WebException) { FtpManage.SetLog("获取列表超时,等候1秒后重试!"); downloadFiles = null; System.Threading.Thread.Sleep(1000); if (conut == 0) { goto Ftp_lbl_02; } conut--; goto Ftp_lbl_01; } catch (Exception ex) { MSG.Show(ex.Message, Global.GetRS["msgTilteError"], MessageBoxButton.OK, MsgIco.Error); FtpManage.SetLog("命令执行失败,原因:" + ex.Message); downloadFiles = null; return downloadFiles; } Ftp_lbl_03: StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名 string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append(" "); line = reader.ReadLine(); } if (result.Length == 0) { return null; } // to remove the trailing ' ' result.Remove(result.ToString().LastIndexOf(' '), 1); reader.Close(); response.Close(); FtpManage.SetLog("命令已成功执行"); return result.ToString().Split(' '); Ftp_lbl_04: FtpManage.SetLog(ReplyInfo); return null; Ftp_lbl_02: FtpManage.SetLog("550 获取列表失败,无法连接远程服务器!"); FtpManage.ftpmanage.IsRefurbish = true; return null; } /// <summary> /// 获取指定目录下的文件和文件夹。 /// </summary> /// <param name="path">要获取的目录</param> /// <returns></returns> public string[] GetFileList(string path)//从ftp服务器上获得文件列表 { return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectory); } /// <summary> /// 获取指定目录下的文件和文件夹。 /// </summary> /// <returns></returns> public string[] GetFileList()//从ftp服务器上获得文件列表 { return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/", WebRequestMethods.Ftp.ListDirectory); } /// <summary> /// 获取目录和文件名,返回目录表。 /// </summary> /// <param name="path">要获取的目录</param> /// <returns></returns> public string[] GetCatalog_FileList(string path) { string[] fountainhead = GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails); string[] Catalog = null; if (fountainhead == null) { return null; } Catalog = new string[fountainhead.Length]; for (int i = 3; i < fountainhead.Length; i++) { Catalog[i - 3] += fountainhead[i].Substring(55, fountainhead[i].Length - 55) + "&";//FileName Catalog[i - 3] += fountainhead[i].Substring(30, 12) + "&";//FileSize Catalog[i - 3] += fountainhead[i].Substring(42, 13) + "&";//AmendDate Catalog[i - 3] += fountainhead[i].Substring(0, 3) + "&"; } return Catalog; } 追问 问题解决了,获益良多 用WebRequestMethods.Ftp.ListDirectoryDetails; 获取的string判断第一个字符是不是 'd',如果是 d 就是 目录 就是不知道为什么微软不再封装一下,毕竟对获取的 detail string使用 substring来得到需要的信息,感觉还是 hardcode,如果对detail string不熟悉的话,也想不到这个方法