是否可以使用StreamWriter c#水平追加文本?

本文关键字:追加 文本 水平 可以使 StreamWriter 是否 | 更新日期: 2023-09-27 18:10:38

我使用Kinect,并且可以将跟踪坐标保存到文本文件中,但是为了绘制图形,我需要为每个X和Y坐标水平地(从左到右)附加文本。现在文本看起来像这样:

2:58:22 PM
Head X      314
Head Y      250
2:58:22 PM
Head X      314
Head Y      251

但是我想把它变成这样:

2:58:22 PM               2:58:23 PM
Head X  1   2   3   4  5   6   7  8  9 
Head Y  1   2   3   4  5   6   7  8  9 
下面是我保存到文本文件的代码:
StreamWriter sw = File.AppendText("Text.txt");
sw.WriteLine(DateTime.Now.ToLongTimeString());
sw.WriteLine("Head X   " + "'t" + TextBox21.Text);
sw.WriteLine("Head Y   " + "'t" + TextBox41.Text);
sw.WriteLine("Shoulder X" + "'t" + TextBox22.Text);
sw.WriteLine("Shoulder Y" + "'t" + TextBox42.Text);
sw.WriteLine("ShoulderLeft X" + "'t" + TextBox23.Text);
sw.WriteLine("ShoulderLeft Y" + "'t" + TextBox43.Text);
sw.WriteLine("ShoulderRight X" + "'t" + TextBox24.Text);
sw.WriteLine("ShoulderRight Y" + "'t" + TextBox44.Text);
sw.WriteLine("ElbowLeft X" + "'t" + TextBox25.Text);
sw.WriteLine("ElbowLeft Y" + "'t" + TextBox45.Text);
sw.WriteLine("ElbowRight X" + "'t" + TextBox26.Text);
sw.WriteLine("ElbowRight Y" + "'t" + TextBox46.Text);
sw.WriteLine("WristLeft X" + "'t" + TextBox27.Text);
sw.WriteLine("WristLeft Y" + "'t" + TextBox47.Text);
sw.WriteLine("WristRight X" + "'t" + TextBox28.Text);
sw.WriteLine("WristRight Y" + "'t" + TextBox48.Text);
sw.WriteLine("HandLeft X" + "'t" + TextBox29.Text);
sw.WriteLine("HandLeft Y" + "'t" + TextBox49.Text);
sw.WriteLine("HandRight X" + "'t" + TextBox30.Text);
sw.WriteLine("HandRight Y" + "'t" + TextBox50.Text);
sw.WriteLine("Spine X   " + "'t" + TextBox31.Text);
sw.WriteLine("Spine Y   " + "'t" + TextBox51.Text);
sw.WriteLine("HipCenter X" + "'t" + TextBox32.Text);
sw.WriteLine("HipCenter Y" + "'t" + TextBox52.Text);
sw.WriteLine("HipLeft X" + "'t" + TextBox33.Text);
sw.WriteLine("HipLeft Y" + "'t" + TextBox53.Text);
sw.WriteLine("HipRight X" + "'t" + TextBox34.Text);
sw.WriteLine("HipRight Y" + "'t" + TextBox54.Text);
sw.WriteLine("KneeLeft X" + "'t" + TextBox35.Text);
sw.WriteLine("KneeLeft Y" + "'t" + TextBox55.Text);
sw.WriteLine("KneeRight X" + "'t" + TextBox36.Text);
sw.WriteLine("KneeRight Y" + "'t" + TextBox56.Text);
sw.WriteLine("AnkleLeft X" + "'t" + TextBox37.Text);
sw.WriteLine("AnkleLeft Y" + "'t" + TextBox57.Text);
sw.WriteLine("AnkleRight X" + "'t" + TextBox38.Text);
sw.WriteLine("AnkleRight Y" + "'t" + TextBox58.Text);
sw.WriteLine("FootLeft X" + "'t" + TextBox39.Text);
sw.WriteLine("FootLeft Y" + "'t" + TextBox59.Text);
sw.WriteLine("FootRight X" + "'t" + TextBox40.Text);
sw.WriteLine("FootRight Y" + "'t" + TextBox60.Text);
sw.Close();         

是否可以使用StreamWriter c#水平追加文本?

sw.Write将允许您编写内联文本。

WriteLine创建一个换行符

跳过WriteLine的"Line"部分,只使用Write?

我可能在这里错了,但我认为也许你只是想使用Write()而不是WriteLine()

WriteLine()将在末尾添加一个换行符。所以你需要做的是:

sw.Write("Head X   " + "'t" + TextBox21.Text);

您可能还需要考虑添加一个额外的分隔符,但这取决于您需要的输出。