How to Append User Inputs to external text file

Trianne

New member
Joined
Aug 7, 2018
Messages
2
Programming Experience
Beginner
Hi Everyone,

I am new to this forum and this is my first post. Something fascinates me about learning a language and I thought I give it a try.

Anyway I want to start with something simple but I can't figure out how to append user input to external file.

This is what I have so far. I can output to screen but do not know how to output to an existing file. Also how do I include a comma to seperate the first and last name.
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:\C#\Projects\test.txt";


            File.ReadAllLines(filePath);


            Console.Write("Please enter your first name? ");
            string FirstName = Console.ReadLine();


            Console.Write("Please enter your last name? ");
            string LastName = Console.ReadLine();


            Console.Write(FirstName + " " +LastName);


            Console.ReadLine();
        }
    }
}

My text file read as follow.
----------
John,Smith
Tom, Reed
----------

Thank You.
Trianne
 
Last edited by a moderator:
Firstly, I have fixed the formatting of your code snippet. Please use appropriate formatting tags when posting code for readability.

Secondly, your call to File.ReadAllLines is pointless. You don't do anything with the lines it returns so why get them? If you were going to use the result, you'd need to assign the String array returned to a variable or the like.

As for the question, there are three options that come to mind.

1. If you do need to use the current contents of the file somehow, you can read that data and append to that, then save the lot, e.g.
var lines = File.ReadAllLines(filePath).ToList();

lines.Add("new line here");

File.WriteAllLines(filePath, lines);

2. Use a StreamWriter, e.g.
using (var writer = new StreamWriter(filePath, true))
{
    writer.WriteLine("new line here");
}

The second parameter to the constructor indicates that text should be appended rather than overwriting what's there. You just need to be careful with where you put line breaks. If you use WriteLine every time then you'll always add a line break to the end, so new text will always start on a new line. If there's no line break at the end of the current file contents though, you will have to write one or else the text you append will start on the same line as the last text in the file.
3. Use AppendAllLines or AppendAllText, e.g.
// Append one new line.
File.AppendAllText(filePath, "new line here");

var newLines = new[] {"new line here", "new line here"};

// Append multiple new lines.
File.AppendAllLines(filePath, newLines);

AppendAllText requires the same consideration as a StreamWriter with regards to line breaks. I'm not 100% sure what AppendAllLines does but I think that it always starts on a new line and does not add a line break after the last new line. You can test that for yourself though.
 
Back
Top Bottom