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

C# 将格式化字符串写入文本文件?,c#,file-io,C#,File Io,C#和一般编程都是全新的 我花了很长时间在谷歌上找到这个问题的答案。我已经看到了几十个关于如何将字符串写入文本文件的结果,但我无法确定的是,是否可以将格式化字符串写入文本文件(当然,还可以保留其格式)。现在,我的程序编写了几行我想用作日志文件头的信息。我不认为我可以使用Console.SetOut,因为我只想写一次这个头文件,然后只想在这个文本文件后面附加新信息(同样以格式化字符串的形式) 这里是控制台输出。此输出的格式与我希望看到的格式完全相同,并写入程序还创建的文本文件中: 以及编写该输出

C#和一般编程都是全新的

我花了很长时间在谷歌上找到这个问题的答案。我已经看到了几十个关于如何将字符串写入文本文件的结果,但我无法确定的是,是否可以将格式化字符串写入文本文件(当然,还可以保留其格式)。现在,我的程序编写了几行我想用作日志文件头的信息。我不认为我可以使用Console.SetOut,因为我只想写一次这个头文件,然后只想在这个文本文件后面附加新信息(同样以格式化字符串的形式)

这里是控制台输出。此输出的格式与我希望看到的格式完全相同,并写入程序还创建的文本文件中:

以及编写该输出的代码:

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
            }
        }
static void Main(字符串[]args)
{
字符串ut=“1Y,4D,01:23:45”;
字符串met=“T+4D,01:11:32”;
字符串mission=“Kapollo”;
字符串vesselName=“土星K”;
字符串vesselModules=“命令/服务模块;Munar漂移模块”;
string kerbals=“Valentina Kerman,飞行员;Jebediah Kerman,飞行员;Bill Kerman,工程师;Bob Kerman,科学家\n”;
字符串[]头部名称={“UT:”、“MET:”、“任务:”、“船舶:”、“模块:”、“机组:”};
string[]headerData=新字符串[6]{ut,met,mission,vesselName,vesselModules,kerbals};
Console.WriteLine(“任务日志计算机已初始化\n”);
对于(int index=0;index1)//如果字符串中有多个项,请执行以下操作:
{
Console.WriteLine(“{0,-12}{1,-46}”,headerNames[index],items[0]);//像往常一样写入headerName。不确定items[0]做什么
对于(int i=1;i
您可以使用该类,在该代码段中,我刚刚用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);
}
static void Main(字符串[]args)
{
字符串ut=“1Y,4D,01:23:45”;
字符串met=“T+4D,01:11:32”;
字符串mission=“Kapollo”;
字符串vesselName=“土星K”;
字符串vesselModules=“命令/服务模块;Munar漂移模块”;
string kerbals=“Valentina Kerman,飞行员;Jebediah Kerman,飞行员;Bill Kerman,工程师;Bob Kerman,科学家\n”;
字符串[]头部名称={“UT:”、“MET:”、“任务:”、“船舶:”、“模块:”、“机组:”};
string[]headerData=新字符串[6]{ut,met,mission,vesselName,vesselModules,kerbals};
StringBuilder=新的StringBuilder(“任务日志计算机已初始化”);
AppendLine();
对于(int index=0;index1)//如果字符串中有多个项,请执行以下操作:
{
builder.AppendFormat(“{0,-12}{1,-46}”,headerName[index],items[0]);//像往常一样写入headerName。不确定items[0]做什么
AppendLine();
对于(int i=1;i字符串结果=builder.ToString();
WriteAllText(“文件路径”,结果);
}

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

YourProgramName >textFileName.txt

不清楚您想要什么;是否要将格式化输出发送到文本文件而不是命令窗口?此外?是否要将不同的内容写入文本文件而不是命令窗口?有什么不同?@DourHighArch抱歉,是的,我想将此信息格式化为文本文件而不是写入控制台。“string result=builder.ToString();//将结果写入文件”我现在有点不清楚为了附加文件我到底使用了什么。我仍然使用file.AppendAllText(路径,);-其中path显然是我要写入的文件的路径,应该是“result”?@blorgon是否要将结果附加到现有文件或创建新文件?是否要将结果写入一个新文件,即我已经编写的函数。我使用file.AppendAllText,因为我假设我应该在或中使用它为了不从文件中删除任何内容,只向其添加新内容。工作非常出色。非常感谢!您的欢迎。我更新了我的答案,说明如何将其写入文件,它将创建新文件或替换现有文件。我有一个新问题