如何将checkedlistbox里选中的内容存入txt文件?

lulu0925 2012-07-10 08:33:07
请教大侠:

如何创建一个新的TXT文件并将checkedlistbox里选中的内容存入txt文件? 然后每项后面自动回车换行。
...全文
129 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lulu0925 2012-07-10
  • 打赏
  • 举报
回复
谢谢 Return_false, 承蒙指教,搞定。。。。散分了。
lulu0925 2012-07-10
  • 打赏
  • 举报
回复
我用了如下代码, 但是总是显示最后的一个,请教大家看看,哪里有问题:

FileStream NewIpFile = new FileStream("D:\\FilePath\\NewIpFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter NewIpFileWriter = new StreamWriter(NewIpFile);
NewIpFileWriter.Flush();
NewIpFileWriter.BaseStream.Seek(0, SeekOrigin.Begin);
NewIpFileWriter.Write(ChkboxIP.);
NewIpFileWriter.Flush();
NewIpFileWriter.Close();
lulu0925 2012-07-10
  • 打赏
  • 举报
回复
dalmeeme,你的代码有错啊。listitem 和 item.selected是语法错误。
  • 打赏
  • 举报
回复
string path = @"C:\Users\M\Desktop\Test1.txt";//设置要创建的txt路径
StringBuilder sb = new StringBuilder();
foreach (var item in this.checkedListBox1.CheckedItems)//循环选中项目
{
sb.Append(item);
sb.Append("\r\n");
}
if (!File.Exists(path))//如果文件不存在
{
using (StreamWriter sw = File.CreateText(path))//不存在则创建
{
sw.WriteLine(sb.ToString());//写入
}

}
dalmeeme 2012-07-10
  • 打赏
  • 举报
回复
哦,看错了,你这个是winform的。
dalmeeme 2012-07-10
  • 打赏
  • 举报
回复
		string s = "";
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
s += item.Text + "\r\n";
}
File.WriteAllText(Server.MapPath("~/test.txt"), s.Trim());

110,536

社区成员

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

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

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