ListBox简单问题

yuehuaqishi 2003-09-03 04:57:35
小弟初学C#
如何保存ListBox中的内容到某个文件中
简单的例子:用ListBox1.Items.Add(***),加了不少行文字
最后需要保存这些文字到一个制定的文件中
最好有例子供学习
谢谢
...全文
16 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuehuaqishi 2003-09-03
  • 打赏
  • 举报
回复
谢谢了
结帖
cnhgj 2003-09-03
  • 打赏
  • 举报
回复
晕~~!慢了,用一楼的方法,方便
cnhgj 2003-09-03
  • 打赏
  • 举报
回复
晕~~!慢了,用一楼的方法,方便
qwbyxw 2003-09-03
  • 打赏
  • 举报
回复
用串行化可直接保存ListBox1.Items到文件中。

Stream stream = null;
FileStream fs = null;
try
{
stream = File.Open(sMTableName+".cfg", FileMode.OpenOrCreate, FileAccess.Write);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, ListBox1.Items); }
catch(Exception excep)
{
。。。
}
finally
{
if (stream != null)
stream.Close();
if (fs != null)
fs.Close();
}

如果保存有格式要求,可参考
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemIOFileStreamClassTopic.htm

心雨楼 2003-09-03
  • 打赏
  • 举报
回复
StreamWriter sw = File.CreateText("D:\\dsaf.txt");

for(int i=0;i<=this.ListBox1.Items.Count-1;i++)
{
sw.WriteLine(this.ListBox1.Items[i].Text.ToString());

}
sw.Close();
心雨楼 2003-09-03
  • 打赏
  • 举报
回复
如上所说的
luaiping 2003-09-03
  • 打赏
  • 举报
回复
StreamWriter sw = File.CreateText("D:\\dsaf.txt");

for(int i=0;i<=this.ListBox1.Items.Count-1;i++)
{
sw.WriteLine(this.ListBox1.Items[i].Value);

}
sw.Close();

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧