如何在一行中写多个字符串?

时间:2019-07-30 09:17:17

标签: c# wpf

我使用SaveFileDialogOpenFileDialog保存我们的数据并重新打开。我使用write命令执行此操作。我将所有数据保存为txt格式,但是我想先描述一下数据。我怎么写?

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    if (sfd.ShowDialog() == true)
    {
        using (StreamWriter write = new StreamWriter(File.Create(sfd.FileName)))
        {
            write.WriteLine(Sertifikasyon.Text);
            write.WriteLine(ACType.Text);
            write.WriteLine(FlightType.Text);
            write.WriteLine(Malzeme.Text);
            write.WriteLine(Motor.Text);
            write.WriteLine(Kcomp.Text);
            write.WriteLine(Wcrew.Text);
            write.WriteLine(Nseat.Text);
            write.WriteLine(Wbaggage.Text);
            write.WriteLine(Vs.Text);
            write.WriteLine(Vcr.Text);
            write.WriteLine(Vltr.Text);
            write.WriteLine(Vclmb.Text);
            write.WriteLine(R.Text);
            write.WriteLine(E.Text);
            write.WriteLine(Eclmb.Text);
            write.WriteLine(Ccr.Text);
            write.WriteLine(Cltr.Text);
            write.WriteLine(Cclmb.Text);
            write.WriteLine(LDcr.Text);
            write.WriteLine(LDltr.Text);
            write.WriteLine(LDclmb.Text);
            write.WriteLine(npcr.Text);
            write.WriteLine(npltr.Text);
            write.WriteLine(npclmb.Text);
            write.WriteLine(Runfuel.Text);
            write.WriteLine(Rresfuel.Text);
            write.Close();
            write.Dispose();
        }
    }
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == true)
    {
        using (StreamReader read = new StreamReader(File.OpenRead(ofd.FileName)))
        {
            Sertifikasyon.Text = read.ReadLine();
            ACType.Text = read.ReadLine();
            FlightType.Text = read.ReadLine();
            Malzeme.Text = read.ReadLine();
            Motor.Text = read.ReadLine();
            Kcomp.Text = read.ReadLine();
            Wcrew.Text = read.ReadLine();
            Nseat.Text = read.ReadLine();
            Wbaggage.Text = read.ReadLine();
            Vs.Text = read.ReadLine();
            Vcr.Text = read.ReadLine();
            Vltr.Text = read.ReadLine();
            Vclmb.Text = read.ReadLine();
            R.Text = read.ReadLine();
            E.Text = read.ReadLine();
            Eclmb.Text = read.ReadLine();
            Ccr.Text = read.ReadLine();
            Cltr.Text = read.ReadLine();
            Cclmb.Text = read.ReadLine();
            LDcr.Text = read.ReadLine();
            LDltr.Text = read.ReadLine();
            LDclmb.Text = read.ReadLine();
            npcr.Text = read.ReadLine();
            npltr.Text = read.ReadLine();
            npclmb.Text = read.ReadLine();
            Runfuel.Text = read.ReadLine();
            Rresfuel.Text = read.ReadLine();                
            read.Close();
            read.Dispose();
        }
    }
}

我想要标题,例如:

Crew Weight=82

这里是txt输出。

Light Sport Aircraft
Tek Motorlu Pervaneli Uçak
Tip A
Kompozit
Atmosferik Piston Motor
0,866
82
2
10
19,5
51,25
40
30
1100
1
0,3
0,45
0,45
0,45
12
12
12
0,7
0,7
0,7
1
5

1 个答案:

答案 0 :(得分:0)

让我们摆脱StreamWriter的影响,而改用File.WriteAllLines字符串插值(诸如$"...{}..."这样的字符串)将帮助我们以所需的格式进行书写:

using (SaveFileDialog sfd = new SaveFileDialog()) {
  if (sfd.ShowDialog())
    File.WriteAllLines(sfd.FileName, new String[] {
      $"Sertifikasyon={Sertifikasyon.Text}",
      $"Type={ACType.Text}",
      $"Flight Type={FlightType.Text}",
      ...
      $"Fuel={Rresfuel.Text}"
    });
}