Tuesday, November 10, 2015

Save & output using a .txt file

using System;
using System.Text;
using System.IO;

public class Sample2 {
  public static void Main(string[] args) {
        bool isSecond = false;
        string path ="";

        while(true){

          if(!isSecond){
            Console.Write("Give me a path for the text file.\n");
            path = Console.ReadLine();
          }

        Encoding enc = Encoding.GetEncoding("Shift_JIS");
        StreamReader sr = new StreamReader(path, enc);
        string text = sr.ReadToEnd();

        sr.Close();

        Console.Write("Saved data: \n" + text);

        Console.Write("To stop this program, write [/stop].\nWrite something: \n");

        StreamWriter writer = new StreamWriter(@path, true, enc);
        string word = Console.ReadLine();
              if(word.Equals("/stop") || word.Equals("/Stop")){
                break;
              }    
     
        writer.WriteLine(word);
        Console.Write("Saved it\n");

        writer.Close();
        isSecond = true;

        }
      Console.Write("Program successfully stopped.\nThe text file will be opened.\n");
      System.Diagnostics.Process.Start(@path);
      return;
  }
}