1

I´m currently trying to improve my c# skills and want to make a check-in system for workers. I want to save the timestamps in a text file and add every new stamp in the same personal .txt file.

My problem is not making it work, my problem is when I write out my list the line System.Collections.Generic.List1[System.String]` is added for every text I add. Please Help me solve this problem.

I don´t really know how to get rid of the System.Collections.Generic.List1[System.String]` part

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Provar_på_IO_File_system
{
    class Program
    {
        static void Main(string[] args)
        {
            string workerName = "Albert Einstien";
            string date = "2019-10-17";
            string time = "14.29";

            if (File.Exists(workerName + ".txt"))
            {
                string line;
                StreamReader sr = new StreamReader(workerName + ".txt");

                List<string> readLines = new List<string>();
                line = sr.ReadLine();

                while (line != null)
                {
                    readLines.Add(line);

                    line = sr.ReadLine();
                }
                sr.Close();

                using (StreamWriter sw = File.AppendText(workerName + ".txt"))
                {
                    sw.WriteLine(readLines);
                    sw.WriteLine("HELLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOO");
                    sw.Close();
                }
            } 
            else
            {
                FileStream fs = new FileStream(workerName + ".txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine(date + "\t" + time + "\t" + workerName);
                sw.Close();
                fs.Close();
            }
        }
    }
}

So the result I'm planning to get from this is to see if just that worker has a text file. If it has none it will create a personal file for that person and add the timestamps for him. Otherwise, if the file already, exists (he have already check-in at least once) the program will read the .txt file, save every line into a list and after that write everything that stod in the file when the system opened it but also add the new timestamp.

Everything works as I like but it doesn't only add the timestamp, the program adds the line System.Collections.Generic.List1[System.String]" and then the timestamps.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Its Rkaj
  • 29
  • 1
  • 4
  • You can't just `sw.WriteLine(readLines);` That's where your problem is. Try something like `sw.WriteLine(string.Join(Environment.NewLine, readLines));` instead or `foreach (string readLine in readLines) sw.WriteLine(readLine);` You shouldn't need to write existing lines though if you're appending in the first place. – itsme86 Oct 17 '19 at 20:14
  • 1
    Why are you trying to read all lines and then write them back to the file? Why not just write the line to the file? – Trevor Oct 17 '19 at 20:15
  • `readLines` is an array, not a string. `sw.WriteLine(readLines)` calls `sw.WriteLine(readLines.ToString())` which does not do what you think it does. The #1 thing you can do to “improve your C# skills” is to [learn how to use a debugger](https://idownvotedbecau.se/nodebugging/). – Dour High Arch Oct 17 '19 at 20:17
  • 1
    Please refrain from adding things like "SOLVED" to the title of your question. See https://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question Instead, accept the answer you have below. –  Oct 17 '19 at 20:46

1 Answers1

1

You would need to loop through the List readLines.

var fileName = string.Format("{0}.txt", workerName)
using (StreamWriter sw = File.AppendText(fileName)) {

   foreach(string line in readLines) {
      sw.WriteLine(line);
   }

}
Tom
  • 11
  • 1
  • 1
    Actually I agree with the comments above... You shouldn't have to write all the lines again, just append. And to add to what Dour High Arch said about "improving your C# skills", I would say search for your question and read through the tons of answers that you will find on stack overflow... There are a lot of helpful people here!!! – Tom Oct 17 '19 at 20:30
  • Thank you very much all! Every comment help a lot and you were right. I dont need to rewrite the existing files agains due to me using the attribute AppendText(). Removing the sw.Write(readLines) made it work. Thank yuo all very much! – Its Rkaj Oct 17 '19 at 20:43