1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| private const string NOT_FOUND = "Not found";
private const string ALREADY_EXISTS = "Already exists";
private const string DOWNLOADED = "Downloaded";
internal static List<DownloadResult> GetTXTFiles(string searchMask, bool bOnlyIfNew)
{
try
{
// Build URI
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "ftp";
uriBuilder.Host = "Server";
uriBuilder.UserName = "Login";
uriBuilder.Password = "Password";
uriBuilder.Path = "/../folder1/folder2/" + searchMask;
// Init list directory web request
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uriBuilder.Uri);
ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpWebRequest.KeepAlive = false;
StreamReader reader = null;
StreamWriter writer = null;
FtpWebResponse response = null;
List<string> foundFiles = null;
try
{
// Look for files matching the mask
response = (FtpWebResponse)ftpWebRequest.GetResponse();
reader = new StreamReader(response.GetResponseStream());
foundFiles = reader.ReadToEnd().Split('\n').ToList();
foundFiles.RemoveAll(string.IsNullOrWhiteSpace);
}
finally
{
if (response != null)
response.Close();
if (reader != null)
reader.Close();
}
if (foundFiles != null && foundFiles.Count > 0)
{
// Sort the results
foundFiles.Sort();
// Browse the results
List<DownloadResult> downloadResults = new List<DownloadResult>();
foreach (string remoteFileName in foundFiles)
{
// Preliminary checks
if (remoteFileName.Contains("No such file"))
continue;
// Check if the file already exists
string localFileName = Path.Combine("C:/temp", remoteFileName);
if (bOnlyIfNew && File.Exists(localFileName))
downloadResults.Add(new DownloadResult("TXT", searchMask, Path.GetFileName(localFileName), ALREADY_EXISTS));
else
{
try
{
// Build download request
UriBuilder newUri = new UriBuilder(uriBuilder.Uri);
newUri.Path = newUri.Path.Replace(searchMask, remoteFileName.Replace("\r", string.Empty));
ftpWebRequest = (FtpWebRequest)WebRequest.Create(newUri.Uri);
ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
// Look for files matching the mask
response = (FtpWebResponse)ftpWebRequest.GetResponse(); // Erreur "501" à l'exécution de cette ligne
writer = new StreamWriter(localFileName, false, Encoding.UTF8);
writer.Write(response.GetResponseStream());
downloadResults.Add(new DownloadResult("TXT", searchMask, Path.GetFileName(localFileName), DOWNLOADED));
}
finally
{
if (writer != null)
writer.Close();
}
}
}
return downloadResults;
}
// Nothing has been found
return new List<DownloadResult>() { new DownloadResult("TXT", searchMask, searchMask, NOT_FOUND) };
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("file not found") || ex.Message.Contains("(550)"))
return new List<DownloadResult>() { new DownloadResult("TXT", searchMask, searchMask, NOT_FOUND) };
else
throw ex;
}
}
} |
Partager