简体   繁体   中英

How can i write more than one string in one line?

I use SaveFileDialog and OpenFileDialog to save our data and reopen it. I use write command for this operation. I save all data in txt format but I want to description before data. How can I write?

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

I want the headline before the values for example:

Crew Weight=82

Here the txt output.

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

Let's get rid of Stream s and Writer s and use File.WriteAllLines instead. String interpolation (strings like $"...{}..." ) will help us to write in desired format:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM