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

本文关键字:路径 格式 FTP 不支持 | 更新日期: 2023-09-27 18:35:16

这是我的代码我有一个包含许多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
                      // ...
               }
          }
     }
    

The given path's format is not supported.这一行得到了这个异常: using (var file = File.OpenRead("ftp://" +FTPAddress +"/" + fileNameInFTP))你能帮忙

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

你应该使用这样的东西,而不是尝试使用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();  

你不能使用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;
    }