2013-03-05 4 views
2

FTP 서버에서 파일을 다운로드하는 코드는 여기에 있습니다. 대소 문자를 구별합니다. 대소 문자를 구분하여 다운로드 할 수 있도록 도와주세요.File in case with encencitive match C#

예 : "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>(); 

답변

2

우리스는 케이스 센세티브이며, 파일에 대문자 비인가 접근을 허용하는 것은 서버에 달려 있습니다. 나는. 전통적으로 대부분의 HTTP 서버는 Uri의 경로 부분에서 대소 문자를 무시하지만 쿼리 부분에서는 대개의 경우를 존중합니다.

귀하의 경우에는 액세스하는 FTP 서버가 파일 이름에 대소 문자 일치를 적용하는 것처럼 보입니다 (Unix/Linux 서버에서 일반적 임). 또한 대소 문자가 다른 여러 파일이있을 수 있습니다.

올바른 구현은 내용을 먼저 나열하고 파일 목록에서 파일 이름을 선택하는 것입니다. how to: List Directory Contents with FTP 도움말에서이 단계를 설명합니다. 그때 우리는 그 디렉토리에있는 파일에 액세스 할 수있는 경우 일치하는 존재입니다 디렉토리를 알고있는 경우

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/"); 
request.Credentials = new NetworkCredential ("anonymous","[email protected]"); 
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()); 
    } 
+0

이 유용 ->를 나열 -> 그것을 일치 ->을 다운로드 -> 즉 하나의 선택. 디렉토리가 대소 문자와 일치하지 않으면 어떻게 디렉토리 목록에 액세스 할 수 있습니까? 이제는 이와 같은 모든 디렉토리를 처리하는 데 오랜 시간이 걸립니다. 어쨌든 당신의 도움에 감사드립니다 :). – Civa