Write formatted strings to text file?
<p>Brand new to C#, and programming in general.</p>

<p>I'm having a heck of a time finding an answer to this question with Google. I've seen dozens of results that cover <em>how</em> to write strings to a text file, but what I can't find out is if it's possible to write a formatted string to a text file (and have it retain its formatting, of course). Right now, my program writes a few lines of information that I want to use as a header for a log file. I don't think I can use Console.SetOut because I only want to write this header once, and then only append the text file with new information (also in the form of formatted strings) thereafter.</p>

<p>So, here's the console output. This output is formatted exactly the way I want to see it written to the text file the program also creates:</p>

<p><a href="https://i.stack.imgur.com/pmXCi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pmXCi.png" alt="enter image description here"></a></p>

<p>And the code that writes that output:</p>

<pre><code>static void Main(string[] args)
    {
        string ut = "1Y, 4D, 01:23:45";
        string met = "T+4D, 01:11:32";
        string mission = "Kapollo";
        string vesselName = "Saturn K";
        string vesselModules = "Command/Service Module;Munar Excursion Module";
        string kerbals = "Valentina Kerman, Pilot;Jebediah Kerman, Pilot;Bill Kerman, Engineer;Bob Kerman, Scientist\n";

        string[] headerNames = { "UT:", "MET:", "Mission:", "Vessel:", "Modules:", "Crew:" };
        string[] headerData = new string[6] { ut, met, mission, vesselName, vesselModules, kerbals };

        Console.WriteLine("Mission Log Computer Initialized\n");

        for (int index = 0; index &lt; headerNames.Length; index++)
        {

            var items = headerData[index].Split(';');   //splits the string when a ';' is present
            if (items.Length &gt; 1)   //if there is more than one item in a string, do this:
            {
                Console.WriteLine("{0,-12} {1,-46}", headerNames[index], items[0]);   //writes headerName as usual. Not sure what items[0] does
                for (int i = 1; i &lt; items.Length; i++)  //for every item in the string, increment 'i' by 1
                {
                    // Render the following items.
                    Console.WriteLine("{0,-12} {1,-46}", string.Empty, items[i]);
                    //^^^writes a blank entry where headerName would print using string.Empty, and then writes 'i', but I'm not sure how 'i' ends up containing multiple strings
                }

            }
            else
            {
                Console.WriteLine("{0,-12} {1,-46}", headerNames[index], headerData[index]);    //otherwise, do this
            }
        }
</code></pre>