Here is my code to download file from ftp server but it's case sensitive match.. could you please help me to download with out case match.
ex: if i'm trying to download "6Gt6hh.xml" but existing file in ftp sever is "6GT6hh.Xml". its not download with my code could you please help me.
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>();
Uris are case sensetive and it is up to server to allow case insensetive acces to files. I.e. traditionally most HTTP servers ignore case in the path portion of Uri, but often respect case in Query portion.
In your case it looks like the FTP server you access enforces case match for file names (common for Unix/Linux servers) and may even have multiple files with different casing.
The correct implementation would be to list content first and than pick the file name from the list of files. The how to: List Directory Contents with FTP article covers this step.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With