将格式化字符串写入文本文件?

时间:2016-05-07 01:25:48

标签: c# file-io

全新的C#,以及一般的编程。

我有一时间在谷歌找到这个问题的答案。我已经看到了几十个结果,其中包含如何将字符串写入文本文件,但我能够发现的是,是否可以编写格式化的字符串到一个文本文件(并保持其格式,当然)。现在,我的程序写了几行信息,我想用它们作为日志文件的标题。我不认为我可以使用Console.SetOut,因为我只想写这个标题一次,然后只在文本文件后附加新信息(也以格式化字符串的形式)。

所以,这是控制台输出。此输出的格式与我希望将其写入程序还创建的文本文件的方式完全相同:

enter image description here

写出该输出的代码:

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 < headerNames.Length; index++)
        {

            var items = headerData[index].Split(';');   //splits the string when a ';' is present
            if (items.Length > 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 < 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
            }
        }

2 个答案:

答案 0 :(得分:1)

您可以使用StringBuilder类,在该代码片段中,我刚刚用StringBuilder.AppendLine和StringBuilder.AppendFormat替换了Console.WriteLine语句:

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 };

    StringBuilder builder = new StringBuilder("Mission Log Computer Initialized");
    builder.AppendLine();

    for (int index = 0; index < headerNames.Length; index++)
    {
        var items = headerData[index].Split(';');   //splits the string when a ';' is present
        if (items.Length > 1)   //if there is more than one item in a string, do this:
        {
            builder.AppendFormat("{0,-12} {1,-46}", headerNames[index], items[0]);   //writes headerName as usual. Not sure what items[0] does
            builder.AppendLine();
            for (int i = 1; i < items.Length; i++)  //for every item in the string, increment 'i' by 1
            {
                // Render the following items.
                builder.AppendFormat("{0,-12} {1,-46}", string.Empty, items[i]);
                builder.AppendLine();
                //^^^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
        {
            builder.AppendFormat("{0,-12} {1,-46}", headerNames[index], headerData[index]);    //otherwise, do this
            builder.AppendLine();
        }
    }

    string result = builder.ToString();
    File.WriteAllText("PATH TO THE FILE", result);
}

答案 1 :(得分:0)

如果您尝试将文本发送到文件而不是命令窗口,请将程序中的所有Console.WriteLine更改为Console.Out.WriteLine。然后从命令窗口运行您的应用程序,如下所示:

YourProgramName >textFileName.txt