110,536
社区成员
发帖
与我相关
我的任务
分享
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());//写入
}
}
string s = "";
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
s += item.Text + "\r\n";
}
File.WriteAllText(Server.MapPath("~/test.txt"), s.Trim());