Windows窗体input/保存到使用Excel的FilePath问题

我有一个简单的窗体与人的详细信息input.I使用文件path来保存条目(mydocuments / .csv文件)我在WF上的显示是一个标签

问题:我做的每个条目都保存在csv / excel文件中,在一个连续的行和标签上,当我点击“显示数据”我希望每个input行在标签显示分离任何帮助将不胜感激,新的这个游戏。 谢谢你我的代码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace StringFilePath { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string FilePath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar+ "mestring.csv"; private void btnAdd_Click(object sender, EventArgs e) { int id = int.Parse(txtID.Text); string fname = TXTfNAME.Text; string lname = txtLname.Text; string phone = txtPhone.Text; string email = txtEmail.Text; StreamWriter sw = File.AppendText(FilePath); sw.Write(id + ","); sw.Write(fname + ","); sw.Write(lname + ","); sw.Write(phone + ","); sw.Write(email + ","); sw.Close(); txtID.Clear(); TXTfNAME.Clear(); txtLname.Clear(); txtPhone.Clear(); txtEmail.Clear(); } private void isplay_Click(object sender, EventArgs e) { string content = File.ReadAllText(FilePath); lblDisplay.Text = content; } 

你需要添加一个Environment.NewLine到你的文本中。 你可以明确地做到这一点,或者只是在最后一个input上使用sw.WriteLine

 string line = $"{id},{fname},{lname},{phone},{email}"; using(StreamWriter sw = new StreamWriter(FilePath)) sw.WriteLine(line); 

或者更简洁一点

 string line = $"{id},{fname},{lname},{phone},{email}{Environment.NewLine}"; File.AppendAllText(FilePath, line); 

对于标签,您应该可以简单地设置其文本与从文件加载的数据,但是,当然,标签应该有足够的高度来显示加载的行