我如何知道路径是文件还是ftp中的目录? [英] How do I know a path is file or is directory in ftp ?

查看:389
本文介绍了我如何知道路径是文件还是ftp中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家...

我的代码如下:

Hi Guys...
I have code like this :

string[] FileList;
StringBuilder result = new StringBuilder();
FtpWebRequest FTP;

FTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://mydomain.com"));
FTP.UseBinary = true;
FTP.Credentials = new NetworkCredential("username", "pass");
FTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = FTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
   string line = reader.ReadLine();
   while (line != null)
   {
       result.Append(line);
       result.Append("\n");
       line = reader.ReadLine();

   }

   result.Remove(result.ToString().LastIndexOf('\n'), 1);
   reader.Close();
   response.Close();
   DosyaListesi = result.ToString().Split('\n');
   for (int x = 0; x < DosyaListesi.Length; x++)
   {
       lbListe.Items.Add(FileList[x].ToString());

       FileAttributes attr = File.GetAttributes(@"ftp://mydomain.com/" + FileList[x].ToString());

          if((attr & FileAttributes.Directory) == FileAttributes.Directory)
               MessageBox.Show("This is a Directory");
          else
               MessageBox.Show("This is a File");
   }



但是这一行出错:


But give a error this row :

FileAttributes attr = File.GetAttributes(@"ftp://mydomain.com/" + FileList[x].ToString());



错误:不支持路径格式



我使用的代码路径是什么文件或是ftp中的目录?

谢谢...


Error : The path format is not supported

What I use code a path is file or is directory in ftp ?
Thanks...

推荐答案

你得到的错误是因为文件。 GetAttributes 不支持网络路径(只有相对路径或绝对路径,如C:\ Temp \ MyFile.txt或S:\Test \readme.txt。



没有直接的方法来确定FTP服务器上的路径是否是文件的目录。唯一可行的解​​决方法是你可以假设一个文件有一个文件结尾而一个目录没有。但是一旦任何目录中的名字中包含一个。,这就会让你陷入困境。



欢呼,

Marco Bertschi
The error you get is because File.GetAttributes does not support network paths (Only relative paths or absulute paths like "C:\Temp\MyFile.txt" or "S:\Test\readme.txt".

There is no direct way to find out if a path on a FTP server is a directory of a file. The only possible workaround is that you can assume that a file has a file ending and a directory has not. But this will get you in trouble as soon as any of the directories contains a "." in its name.

cheers,
Marco Bertschi


我认为你应该使用 WebRequestMethods.Ftp.ListDirectoryDe​​tails 方法而不是你可以解析结果。



http://www.copyandwaste.com/posts/view/parsing-webrequestmethodsftplistdirectorydetails-and-listdirectory/ [<一个href =http://www.copyandwaste.com/posts/view/parsing-webrequestmethodsftplistdirectorydetails-and-listdirectory/target =_ blanktitle =New Window> ^ ]
I think you should use the WebRequestMethods.Ftp.ListDirectoryDetails method instead and then you can parse the results.

See http://www.copyandwaste.com/posts/view/parsing-webrequestmethodsftplistdirectorydetails-and-listdirectory/[^]


这篇关于我如何知道路径是文件还是ftp中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆