!!!重赏之下必有勇夫,求教c# ftp如何判断目录下取出来的东西是文件还是文件夹!!!

wangyue4 2011-08-26 09:16:39

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分啊!
...全文
509 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wj0728 2012-11-14
  • 打赏
  • 举报
回复
<DIR>只有在ftp服务器架设在windows中时才会出现,如果是unix系统,是以dr-xr-xr-x类似开头的,需要分别判断的
Q333111555 2012-07-12
  • 打赏
  • 举报
回复
我的FTP。使用Details也不能得道详细信息中有Dir。 怎么办?
萧炎 2011-08-27
  • 打赏
  • 举报
回复
不是吧都结贴了啊.......
weike021996 2011-08-27
  • 打赏
  • 举报
回复
gogogo跟五楼的一样
cjh200102 2011-08-27
  • 打赏
  • 举报
回复
楼上可以解决
LMAOhuaNL 2011-08-26
  • 打赏
  • 举报
回复
FileInfo fileInfo = new FileInfo(fileName);

//这样判断
if ((fileInfo.Attributes & FileAttributes.Directory) != 0)

{

//目录

}

else

{

//文件

}
蝶恋花雨 2011-08-26
  • 打赏
  • 举报
回复
http://blog.csdn.net/ou8811/article/details/5295780
这里面的就够你用的了。判断一下dir就可以了
蝶恋花雨 2011-08-26
  • 打赏
  • 举报
回复
在使用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
threenewbee 2011-08-26
  • 打赏
  • 举报
回复 1
WebRequestMethods.Ftp.ListDirectoryDetails

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧