Bonjour,

je tente de trouver une solution dans mon code car ici, j'ouvre un fichier txt coreftp.ini dans lequel je lis toutes les lignes que je mets dans un tableau. Ensuite après modification de certaines lignes du tableau, je suis censé enregistrer le fichier avec les données de ce tableau, ca ne fonctionne pas.

Il me dit ne pas avoir accès au fichier pourtant je ferme bien le fichier...


J'ai tenté d'utiliser le FileStream mais ca ne fonctionne pas :/


Quelqu'un aurait une idée?? Merci !

(l'erreur est déclaré a la ligne 205 lors du streamwriter)


EDIT : Sans utiliser le openfiledialog, ca fonctionne. Comment ca se fait? J'ai tenté d'utiliser openfiledialog1.Dispose(); mais ca ne fonctionne pas


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
 
    public partial class Form1 : Form
    {
        List<string> site = new List<string>();
        List<string> Read_fileTAB = new List<string>();
        string section = "";
 
        string Garg1 = "";
        string Garg2 = "";
        string Garg3 = "";
        string Garg4 = "";
        string link_Read1 = "";
        string chemin_fichier1 = "";
        string nom_fichier1 = "";
        int arg1_pos = 0;
        int arg2_pos = 0;
        int arg3_pos = 0;
        int arg4_pos = 0;
 
        public Form1()
        {
            InitializeComponent();
        }
 
 
        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
 
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Fichier de configuration FTP INI (*.ini)|*.ini|All files (*.*)|*.*";
            openFileDialog1.RestoreDirectory = false;
            openFileDialog1.ShowHelp = true;
 
 
 
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (openFileDialog1.OpenFile() != null)
                    {
                        //MessageBox.Show("Fichier ouvert correctement");
                        chemin_fichier1 = openFileDialog1.FileName;
 
 
                        //MessageBox.Show(Path.GetDirectoryName(openFileDialog1.FileName));
                        Read_file(openFileDialog1.FileName,"[FTP-SETTINGS]", "Site", "port", "login", "password");
 
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
 
        }
 
 
        //public string Read_file(string link_Read, string section, string arg1, string arg2, string arg3, string arg4) //ROUTINE TEST
        //{
 
        //    int counter = 0;
        //    string line;
        //    StreamReader file = new StreamReader(link_Read);
        //    while ((line = file.ReadLine()) != null) // Tant qu'il y a des ligne a lire, on continue
        //    {
        //        Read_fileTAB.Add(line);
        //        counter++;
 
        //    }
 
        //    int count = 0;
        //    foreach (string value in Read_fileTAB)
        //    {
        //        while (counter != count)
        //        {
        //            if (Read_fileTAB[count].ToString().Contains(arg1))
        //            {
 
        //                string[] test = Read_fileTAB[count].Split('=');
        //                MessageBox.Show("FTP-IN se trouve en position " + count);
 
 
 
        //            }
        //            count++;
 
 
        //        }
 
        //    }
        //}
 
 
 
 
       // private void button2_Click(object sender, EventArgs e)
        public string Read_file(string link_Read, string section, string arg1, string arg2, string arg3, string arg4) //ROUTINE TEST
        {
            Garg1 = arg1;
            Garg2 = arg2;
            Garg3 = arg3;
            Garg4 = arg4;
            link_Read1 = link_Read;
            int counter = 0;
            string line;
            FileStream fs = new FileStream(link_Read, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 
            StreamReader file = new StreamReader(fs);
            while ((line = file.ReadLine()) != null) // Tant qu'il y a des ligne a lire, on continue
            {
                Read_fileTAB.Add(line);
                counter++;
 
            }
 
            file.Close();
 
 
 
            int count = 0;
            bool result = false;
            foreach (string value in Read_fileTAB)
            {
                while (result != true)
                {
                    if (Read_fileTAB[count].ToString().Contains(section))
                    {
                        count++;
                        while (counter != count)
                        {
                            if (Read_fileTAB[count].ToString().Contains(arg1))
                            {
                                string[] arg1_tab = Read_fileTAB[count].Split('=');
                                textBox1.Text = (arg1_tab.ElementAt(1));
                                arg1_pos = count;
 
 
                            }
                            else if (Read_fileTAB[count].ToString().Contains(arg2))
                            {
                                string[] arg2_tab = Read_fileTAB[count].Split('=');
                                textBox2.Text = (arg2_tab.ElementAt(1));
                                arg2_pos = count;
 
                            }
                            else if (Read_fileTAB[count].ToString().Contains(arg3))
                            {
                                string[] arg3_tab = Read_fileTAB[count].Split('=');
                                textBox3.Text = (arg3_tab.ElementAt(1));
                                arg3_pos = count;
 
                            }
                            else if (Read_fileTAB[count].ToString().Contains(arg4))
                            {
                                string[] arg4_tab = Read_fileTAB[count].Split('=');
                                textBox4.Text = (arg4_tab.ElementAt(1));
                                arg4_pos = count;
 
 
                            }
 
                            else if (Read_fileTAB[count].ToString().Contains("["))
                            {
                                result = true;
                                break;
                            }
 
 
                            count++;
                        }
 
                        result = true;
 
                    }
 
 
 
 
 
                }
 
            }
            return "d";
        }
 
        private void modifier_but1_Click(object sender, EventArgs e)
        {
 
            int count2 = Read_fileTAB.Count();
            if (count2 >= 1)
            {
                Read_fileTAB[arg1_pos] = (Garg1 + "=" + textBox1.Text);                 // Modification dans le tableau des valeurs contenu dans les textbox
                Read_fileTAB[arg2_pos] = (Garg2 + "=" + textBox2.Text);
                Read_fileTAB[arg3_pos] = (Garg3 + "=" + textBox3.Text);
                Read_fileTAB[arg4_pos] = (Garg4 + "=" + textBox4.Text);
                string nom_fichier = Path.GetFileName(link_Read1);
                string chemin_fichier_ss_ext = Path.GetDirectoryName(link_Read1);
                string repertoire_backup = chemin_fichier_ss_ext + "\\backup";
                DirectoryInfo path = Directory.CreateDirectory(repertoire_backup);   //Création répertoire backup
                File.Copy(Path.Combine(chemin_fichier_ss_ext, nom_fichier), Path.Combine(repertoire_backup, "coreftp_backup.ini"), true); //copie du fichier dans le repertoire backup
 
                StreamWriter file = new StreamWriter(link_Read1);
                int count_down = 0;
                foreach (string value in Read_fileTAB)
                {
                    file.WriteLine(Read_fileTAB.ElementAt(count_down));
                    count_down++;
 
 
                }
                file.Close();
 
 
 
 
            }
            else MessageBox.Show("Aucune données chargées");
 
 
 
 
       }
 
 
 
 
 
    }
 
}