public string[] GetFileList()
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + "/"));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(username,
                                                           password);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse response = reqFTP.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();
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                downloadFiles = null;
                return downloadFiles;
            }
        }这是我的代码,其功能是获得ftp目录下的所有文件和文件夹。返回进string数组。
现在我想问遍历返回的这个数组时如何判断其内容是一个文件还是文件夹。
不要说判断名字有没有“.”,这个不好。谢谢大家。200分啊!

解决方案 »

  1.   

    WebRequestMethods.Ftp.ListDirectoryDetails 
      

  2.   

      在使用WebRequestMethods.Ftp.ListDirectoryDetails取得文件夹下所有内容时,会发现如果其中有文件夹,那么文件夹的的详细信息中会有一个"<DIR>"标识,我们就可以通过这个来将其区分开来      同时在获取文件夹以及文件名称时用到WebRequestMethods.Ftp.ListDirectory,这个指令能过只获得文件夹下所有文件包括文件夹的名字,通过这两个指令所获取的信息逐一比较,便能确定出文件或文件夹名以传递到download和downftp方法中http://blog.csdn.net/ou8811/article/details/5295780
    http://topic.csdn.net/u/20080130/23/d65b981a-5d14-4898-9bff-c39bed18a7eb.html
      

  3.   

    http://blog.csdn.net/ou8811/article/details/5295780
    这里面的就够你用的了。判断一下dir就可以了
      

  4.   

    FileInfo fileInfo = new FileInfo(fileName);//这样判断
    if ((fileInfo.Attributes & FileAttributes.Directory) != 0){//目录}else{//文件}
      

  5.   

    gogogo跟五楼的一样
      

  6.   

    我的FTP。使用Details也不能得道详细信息中有Dir。  怎么办?
      

  7.   

    <DIR>只有在ftp服务器架设在windows中时才会出现,如果是unix系统,是以dr-xr-xr-x类似开头的,需要分别判断的