我使用SaveFileDialog和OpenFileDialog来保存我们的数据并重新打开它。我使用 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(); } }}我想要标题在值之前,例如:Crew Weight=82这里是txt输出。
1 回答
温温酱
TA贡献1752条经验 获得超4个赞
让我们摆脱Stream
s 和Writer
s 并使用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}"
});
}
- 1 回答
- 0 关注
- 136 浏览
添加回答
举报
0/150
提交
取消