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 97 98 99 100
|
public void lectureFichier(string fichier, string[] words, string Dossier)
{
try
{
// Création d'une instance de StreamReader pour permettre la lecture de notre fichier
StreamReader monStreamReader = new StreamReader(fichier);
string ligne = monStreamReader.ReadLine();
string dossier;
string fichierAdl;
//string cible;
// Lecture de toutes les lignes et affichage de chacune sur la page
while (ligne != null)
{
if (Dossier == "0")
{
dossier = ligne.Substring(0, 1);
MessageBox.Show(dossier);
if (dossier != "d")
{
MessageBox.Show("rentrer dans le if pour les fichier");
fichierAdl = ligne.Substring(46, ligne.Length - 46);
string url = words[0] + "/" + fichierAdl;
MessageBox.Show(url);
Uri serverUri = new Uri(url);
FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri);
myRequest.Credentials = new NetworkCredential(words[2], words[1]);
myRequest.KeepAlive = true;
myRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)myRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream fs = new FileStream("C:/ftp/" + fichierAdl, FileMode.Create);
byte[] buffer = new byte[2000];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
fs.Flush();
} while (!(read == 0));
response.Close();
responseStream.Close();
fs.Close();
}
else
{
MessageBox.Show("rentrer dans le else pour les dossier");
string DossierUrl = ligne.Substring(46, ligne.Length - 46);
MessageBox.Show(DossierUrl);
if ((DossierUrl == ".") || (DossierUrl == ".."))
{
MessageBox.Show("dossier . et .. non pris en compte");
}
else
{
MessageBox.Show("verification OK");
string url = words[0] + "/" + DossierUrl;
string activeDir = @"C:\ftp\";
string newPath = System.IO.Path.Combine(activeDir, DossierUrl);
System.IO.Directory.CreateDirectory(newPath);
MessageBox.Show("Le chemin local est:" + newPath);
MessageBox.Show("Le nom du dossier en cours est:" + DossierUrl);
MessageBox.Show("L'url en cours est:" + url);
Uri serverUri = new Uri(url);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri);
txtLog.Text += "\n" + myRequest.RequestUri.ToString() + "\n";
myRequest.Credentials = new NetworkCredential(words[2], words[1]);
myRequest.KeepAlive = false;
myRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)myRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream fs = new FileStream(newPath + "/liste.txt", FileMode.Create);
byte[] buffer = new byte[2000];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
fs.Flush();
} while (!(read == 0));
response.Close();
responseStream.Close();
fs.Close();
lectureFichier(newPath + "/liste.txt", words, DossierUrl);
}
}
ligne = monStreamReader.ReadLine();
}
else
{
MessageBox.Show("parcour Recursif a faire ici");
} |
Partager