Create Score text without overwriting the file

0

The command written below does not create the score text .txt in the directory of the folder where the game is installed. I also needed the code below not to overwrite the file but add the punctuation below the points already recorded. C # Visual Studio Community 2017.

varexe = System.Environment.CurrentDirectory;


StreamWriter writer = new StreamWriter(varexe + "\Score.txt");
                                                                    writer.WriteLine("Player1");
                                                                    writer.WriteLine(tent);
                                                                    writer.Close();
    
asked by anonymous 31.10.2018 / 18:42

1 answer

1

You can use a File.AppendText command, here you will find how to use it: Append Text C #

Code copied from the site I recommended

You choose between:

(1) Write to file

(2) Add to file

(3) Only read the file

 using System;
 using System.IO;

class Test 
 {
   public static void Main() 
   {

     string path = @"c:\temp\MyTest.txt";
     // texto entra uma vez só no arquivo
     if (!File.Exists(path)) 
       {
        //(1) Criando o arquivo e escrevendo nele (Só de exemplo)
        using (StreamWriter sw = File.CreateText(path)) 
         {
            sw.WriteLine("Player1");
            sw.WriteLine("Pontos");
            sw.WriteLine("blablabla");
        }   
    }

    //(2) Aqui adiciona novas informações no texto no final da linha.
    using (StreamWriter sw = File.AppendText(path)) 
    {
            sw.WriteLine("Player1");
            sw.WriteLine("Pontos");
            sw.WriteLine("blablabla");
    }   

    //(3) Aqui abre o arquivo apenas para leitura
    using (StreamReader sr = File.OpenText(path)) 
    {
        string s = "";
        while ((s = sr.ReadLine()) != null) 
        {
            Console.WriteLine(s);
        }
    }
}
}   

I hope I have helped you!

    
31.10.2018 / 18:48