C#FTP不支持给定路径的格式

时间:2014-12-26 12:01:30

标签: c# ftp .net-4.5

这是我的代码 我有一个包含许多zip个文件的FTP。每个zip文件都有一个XML,名称相同。 我想解析这些xml文件。

我做的是这个:

  1. 获取FTP中所有zip文件的列表,并将名称保存在此变量directories中。

  2. 现在我要打开每个zip文件,其名称在directories列表中。我做到了。

    foreach (string fileNameInFTP in directories)                   
    {
    }
    
  3. 现在要阅读该zip文件的内容,我试过了:。

    string fileName =  FTPAddress + fileNameInFTP;
    using (var file = File.OpenRead(fileName))
    using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
    {
          foreach (var entry in zip.Entries)
          {
               using (var stream = entry.Open())
               {
                      // do whatever we want with stream
                      // ...
               }
          }
     }
    
  4. 我在此行中遇到此异常The given path's format is not supported.using (var file = File.OpenRead("ftp://" +FTPAddress +"/" + fileNameInFTP))可以帮助你

2 个答案:

答案 0 :(得分:4)

您应该使用类似的东西,而不是尝试使用File.OpenRead进行远程FTP文件下载。

http://msdn.microsoft.com/en-us/library/ms229711%28v=vs.110%29.aspx

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();

using (var zip = new ZipArchive(responseStream , ZipArchiveMode.Read))
{
   //Loops through each file in the zip that has the ".xml" extension 
   foreach (var entry in zip.Entries.Where(x=> (Path.GetExtension(x.Name) ?? "").ToLower() ==".xml"))
   {
        using (var stream = entry.Open())
        {
            //Load xml file and do whatever you like with it.
            var xmlDocument = XDocument.Load(stream);
        }
    }
}

Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

response.Close();  

答案 1 :(得分:2)

您无法使用File IO打开FTP流,以下是我们如何使用.NET中的WebRequest打开FTP的示例:

    private static void Main(string[] args)
    {
        var ftp = WebRequest.Create(@"ftp://ftp.microsoft.com/softlib/MSLFILES/aspwebwiz2k.zip");
        //ftp.Credentials=new NetworkCredential("anonymous","anonymous");
        var response=ftp.GetResponse();
        var stream=response.GetResponseStream();
        var ms = ToMemoryStream(stream);

        var archive = new ZipArchive(ms, ZipArchiveMode.Read);
        var entry=archive.GetEntry("file name here");

        var doc=XDocument.Load(entry.Open());
    }

    public static MemoryStream ToMemoryStream( Stream stream)
    {
        var memoryStream = new MemoryStream();
        var buffer = new byte[4096];

        while (true)
        {
            var readCount = stream.Read(buffer, 0, buffer.Length);
            if (readCount == 0)
                break;

            memoryStream.Write(buffer, 0, readCount);
        }

        memoryStream.Position = 0;
        return memoryStream;
    }