Search code examples
c#fileindexingfilestreambinarywriter

keep writing at the end of file after re opening it


[in C#]If i keep the file open, I ncan write multiples input and it saves it but if i close it, re-open it and start to write again, it starts from the beggining and overwirte everything. I tryed to open it using FileMode.Append and to do a File.Seek(0, SeekOrigin.End);

Here is the file opening code :

else if (File.Exists((dossier_defaut + nom_lexique)) && File.Exists((dossier_defaut + nom_index))) //si les deux fichiers existent
{
                //ouverture des fichiers
    fs_lxq = File.Open((dossier_defaut + nom_lexique + extLexique), FileMode.Append, FileAccess.ReadWrite);  // Création du fichier
    fs_idx = File.Open((dossier_defaut + nom_index + extIndex), FileMode.Open, FileAccess.ReadWrite);  // Création du fichier
}
else //aucun fichier existe
{
                    //création de deux nouveaux fichiers
    fs_lxq = File.Open((dossier_defaut + nom_lexique + extLexique), FileMode.Create, FileAccess.ReadWrite);  // Création du fichier
    fs_idx = File.Open((dossier_defaut + nom_index + extIndex), FileMode.Create, FileAccess.ReadWrite);  // Création du fichier


}

How can I assure myself that it will allways write at the end?


Solution

  • Ok it was just a stupid mistake in my conditions that checked if the file existed at the oppening of the program I had forgot to add the file extension so it was not finding it and creating new files everytime