请教各位高人
我的程序不断向listbox中写入内容,我想当它写到比如说一万行的时候,把这一万行都保存在txt文档中,形成日志,并在listbox中把这一万行删除,应该如何写代码,多谢
解决方案 »
- ADO.net连接数据库,求人解释
- TextBox 绑定ToolStripMenuItem 后,单击ToolStripButton 怎么判断是在哪个文本框
- 用正则表达式匹配输入的时间
- wcf binding问题。。高手们进!!
- VS2008 怎么让焦点所在的行 高亮.
- 请问怎样可以把vs做成免安装版?
- 有关命名空间的理解
- 求助: 怎样才能把页面里一个DataGrid的样式和数据转化为HTML???
- MulticastDelegate.GetInvocationList能对一个事件用吗?
- 关于:如何从Access中读出数据,并且写入到DataGrid控件当中?
- 求数据查询方法,t-sql、linq to sql不限
- 最近打印和外部调用dll的问题很多,分享一下我的C#OPOS打印经验。
while (index < 10000 && listbox.Items.Count > 0)
{
listbox.Items.RemoveAt(0);
index++;
}
{
ListBox.Items.Add(txt); if(ListBox.Items.Count>=NUM)
{
FileStream fs = new FileStream("文件路径",FileMode.Open);
for(int i=0;i<NUM;i++)
{
//获得字节数组
byte [] data =new UTF8Encoding().GetBytes(ListBox.Items[0].ToString());
//开始写入
fs.Write(data,0,data.Length); ListBox.Items.RemoveAt(0);
}
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}}
使用listbox.Clear()会把还没有存入txt文档的数据也删除吧
1,建立一个事件,基于listbox变化的,应该是listboxchanged吧,该事件监视listbox的行数;
2,当行数>10000时,触发存储动作并删除即可。
多看看事件,及触发机制。
大致思路: listbox_changed(object sender,Eventargs e){
if(num>10000){ // num=listbox.items.count
Save();
Delete();
}
}
改为fs.WriteLine(data,0,data.Length);
报错了错误 “System.IO.FileStream”并不包含“WriteLine”的定义
if (listBox1.Items.Count >= NUM)
{
string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH");
File.WriteAllText(@"c:\日志" + NowTime + ".txt", null);
StreamWriter sw = new StreamWriter(@"c:\日志" + NowTime + ".txt");
for (int i = 0; i < NUM; i++)
{
string data = listBox1.Items[0].ToString();
sw.Write(data);
sw.WriteLine();
listBox1.Items.RemoveAt(0);
}
//清空缓冲区、关闭流
sw.Flush();
sw.Close();
} 呵呵,解决了,谢谢大家了