Bonjour à toutes et à tous.
Dans le but d'un projet de fin d'année de Bts Iris , je me vois dans l'obligation de créer un serveur ftp et de l'intégrer au code de notre application.
Etant assez novice en c#, j'ai un peu de mal à faire ce serveur ftp.
Néanmoins, j'ai réussi à avancer dans mon code, mais malgré tout, une erreur me bloque.
Et voici l'erreur du serveur locale que j'ai installé pour faire mes tests. :
Alors que j'ai pourtant spécifié le login et le mdp.
Voici la page principale du code :
et voici la page de la classe ServeurFtp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
101
102
103
104
105
106 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; namespace AlkernBloc { public partial class Form3 : Form { string Lieu, Adresse_ip; string Ip1, Ip2, Ip3, Ip4; public Form3() { Uri serverUri = new Uri("ftp://127.0.0.1/test.rar"); FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri); myRequest.KeepAlive = false; //maintenir connexion constante myRequest.Method = WebRequestMethods.Ftp.DownloadFile; //Definit si on dl ou ul. myRequest.Timeout = 1000; //Delai d'execution de la requête myRequest.UseBinary = false; FtpWebResponse response = (FtpWebResponse)myRequest.GetResponse(); InitializeComponent(); } private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void Form3_Load(object sender, EventArgs e) { groupBox1.Hide(); checkedListBox1.Hide(); } private void button3_Click(object sender, EventArgs e) { groupBox1.Show(); } private void button4_Click(object sender, EventArgs e) { String S; Lieu = textBoxLieu.Text; Ip1 = textBoxIp1.Text; Ip2 = textBoxIp2.Text; Ip3 = textBoxIp3.Text; Ip4 = textBoxIp4.Text; Adresse_ip = Ip1 + "." + Ip2 + "." + Ip3 + "." + Ip4; S = Lieu + " " + Adresse_ip; checkedListBox1.Show(); checkedListBox1.Items.Add(S); ServeurFtp myFTP = new ServeurFtp { Server = "127.0.0.1/test.rar", Username = "root", Password = "cauchy" }; myFTP.Download("127.0.0.1/test.rar", "test.rar"); } private void button1_Click(object sender, EventArgs e) { } private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e) { } } }
Vous remerciant d'avance pour votre aide , je vous souhaite une bonne journée.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace AlkernBloc { class ServeurFtp { //Proppriétés public string Server { get; set; } public string Username { get; set; } public string Password { get; set; } //Méthode public void Download(string directory, string file) { Uri serverUri = new Uri("ftp://" + this.Server + "/" + directory + "/" +file); //Url du ftp if (serverUri.Scheme != Uri.UriSchemeFtp) { return; } FtpWebRequest myRequest = (FtpWebRequest)FtpWebRequest.Create(serverUri); myRequest.Credentials = new NetworkCredential(this.Username, this.Password); //Definition des login et password myRequest.KeepAlive = false; //Option pour la connection constante myRequest.Method = WebRequestMethods.Ftp.DownloadFile; //Definir si on download ou upload FtpWebResponse response = (FtpWebResponse)myRequest.GetResponse(); Stream responseStream = response.GetResponseStream(); FileStream fs = new FileStream("C:/" + file, FileMode.Create); byte[] buffer = new byte[2000]; //Taille du fichier 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(); }} }
Partager