【问题标题】:File Download from Ftp server with case insencitive match c#从 Ftp 服务器下载文件,不区分大小写匹配 c#
【发布时间】:2013-02-19 10:37:46
【问题描述】:

这是我从 ftp 服务器下载文件的代码,但它区分大小写匹配.. 请您帮我下载不匹配大小写的文件。

例如:如果我尝试下载“6Gt6hh.xml”但 ftp 服务器中的现有文件是“6GT6hh.Xml”。它不是用我的代码下载的,你能帮帮我吗?

private void Download(string file, ServerCredentials FtpCdrl, string DayOfYear, string dest, string ab)
    {
        try
        {
            string uri = "ftp://" + FtpCdrl.Host + "/" + FtpCdrl.DirPath.Replace("\\", "/") + "/" + DayOfYear + "/" + file;
            Uri serverUri = new Uri(uri);
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {
                return;
            }
            if (!Directory.Exists(dest))
                Directory.CreateDirectory(dest);
            if (!Directory.Exists(dest + "\\" + ab))
                Directory.CreateDirectory(dest + "\\" + ab);
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FtpCdrl.Host + "/" + FtpCdrl.DirPath.Replace("\\", "/") + "/" + DayOfYear + "/" + file));
            reqFTP.Credentials = new NetworkCredential(FtpCdrl.UID, FtpCdrl.Pwd);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Proxy = null;
            reqFTP.UsePassive = false;
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream responseStream = response.GetResponseStream();


            FileStream writeStream = new FileStream(dest + "\\" + ab + "\\" + file, FileMode.Create);
            int Length = 2048;
            Byte[] buffer = new Byte[Length];
            int bytesRead = responseStream.Read(buffer, 0, Length);

            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, Length);
            }
            status("File Downloaded Successfully: .\\" + ab + "\\" + file, Color.Green);
            writeStream.Close();
            response.Close();
            failed = 0;
        }
        catch (WebException wEx)
        {
            failed++;
            status("[Download Error]" + wEx.Message, Color.Red);
            if (failed < 3)
                Download(file, FtpCdrl, DayOfYear, dest, ab);
        }
        catch (Exception ex)
        {
            status("[Download Error]" + ex.Message, Color.Red);
        }
    }
 public class ServerCredentials
    {
        public string Host, UID, Pwd, DirPath;
        public int Pst;
        public string Mail;
        public string facility;
        public string Batchextn;

        public ServerCredentials(string _Host1, string _DirPath1, string _Uid1, string _Pwd1, string _Mail, int _Pst1, string _facility, string _batchextn)
        {
            Host = _Host1;
            UID = _Uid1;
            Pwd = _Pwd1;
            DirPath = _DirPath1;
            Pst = _Pst1;
            Mail = _Mail;
            facility = _facility;
            Batchextn = _batchextn;
        }
    }
    public List<ServerCredentials> Svr = new List<ServerCredentials>();

【问题讨论】:

    标签: c# ftp download case-insensitive


    【解决方案1】:

    Uris 区分大小写,由服务器决定是否允许对文件进行不区分大小写的访问。 IE。传统上,大多数 HTTP 服务器会忽略 Uri 路径部分的大小写,但通常会尊重 Query 部分的大小写。

    在您的情况下,您访问的 FTP 服务器似乎强制文件名的大小写匹配(对于 Unix/Linux 服务器很常见),甚至可能有多个具有不同大小写的文件。

    正确的实现是首先列出内容,然后从文件列表中选择文件名。 how to: List Directory Contents with FTP 文章介绍了这一步。

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    
    using(var response = (FtpWebResponse)request.GetResponse())
      using(var responseStream = response.GetResponseStream())
        using(var reader =  new StreamReader(responseStream))
        {
          Console.WriteLine(reader.ReadToEnd());
        }
    

    【讨论】:

    • 如果我知道存在大小写匹配的目录,这很有用,然后我们可以访问该目录中的文件 --> 列出它 --> 匹配它 --> 选择那个 --> 下载它。如果目录不匹配大小写,我如何访问目录列表。现在像这样做每个目录都需要很长的过程。无论如何感谢您的帮助:)。
    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多