软件编程
位置:首页>> 软件编程>> C#编程>> C#保存listbox中数据到文本文件的方法

C#保存listbox中数据到文本文件的方法

作者:小萝莉  发布时间:2023-02-20 10:37:27 

标签:C#,listbox

本文实例讲述了C#保存listbox中数据到文本文件的方法。分享给大家供大家参考。具体实现方法如下:


private void SaveLstToTxt(ListBox lst)
{
sfd.Filter = "(*.txt)|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
 string sPath = sfd.FileName;
 FileStream fs = new FileStream(sPath, FileMode.Create);
 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
 int iCount = lst.Items.Count - 1;
 for (int i = 0; i <= iCount; i++)
 {
  sw.WriteLine(lst.Items[i].ToString());
 }
 sw.Flush();
 sw.Close();
 fs.Close();
}
}

希望本文所述对大家的C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com