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)
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)
Code:
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");
}
}
}
Aucun commentaire:
Enregistrer un commentaire