Question

[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?

Was it helpful?

Solution 2

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

OTHER TIPS

You can use the AppendText method of a File object. See below:

        using (StreamWriter sw = File.AppendText(path)) 
        {
            sw.WriteLine("This");
            sw.WriteLine("is Extra");
            sw.WriteLine("Text");
        } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top