没有合适的资源?快使用搜索试试~ 我知道了~
枚举使用枚举使用枚举使用枚举使用枚举使用枚举使用枚举使用
资源推荐
资源详情
资源评论
using System.IO;
先是文件的创建 创建一个 new.txt 的文件 在D:\temp\ 目录下:
private void button1_Click(object sender, System.EventArgs e)
{
StreamWriter SW;
SW=File.CreateText(@"D:\temp\new.txt");
SW.WriteLine("test"); // 创建后 写入两行
SW.WriteLine("the second line");
SW.Close();
Console.WriteLine("Creat OK");
}
对文件进行读取.. 并显示在 listbox 里..
private void button1_Click(object sender, System.EventArgs e)
{
StreamReader strm;
try //用 try catch 机制 防止文件不存在 读入失败..
{
strm=File.OpenText(@"D:\temp\new.txt");
string s = strm.ReadLine();
while(s != null )
{
listBox1.Items.Add(s);
s = strm.ReadLine();
}
strm.Close();
Console.WriteLine("Read OK");
}
catch
{
MessageBox.Show("Open file error!");
}
}
读好了 那再用文件的写入 利用AppendText 方法.. 区别在于 假如文件不存在 会自动创建并写入该文件...
private void button3_Click(object sender, System.EventArgs e)
{
StreamWriter SW;
SW = File.AppendText(@"D:\temp\new.txt");
SW.WriteLine("This line is Appended");
SW.Close();
Console.WriteLine("Appended OK");
}
删除存在的文件.. 并清空listbox 内容:
private void button4_Click(object sender, System.EventArgs e)
{
File.Delete(@"D:\temp\new.txt");
listBox1.Items.Clear();
}
另: 以上都是对固定路径进行操作.. 有很多时候需要用户自己选择文件 跳出对话框选择文件:
OpenFileDialog f = new OpenFileDialog();
}
catch
{
MessageBox.Show("Open file error!");
}
}
读好了 那再用文件的写入 利用AppendText 方法.. 区别在于 假如文件不存在 会自动创建并写入该文件...
private void button3_Click(object sender, System.EventArgs e)
{
StreamWriter SW;
SW = File.AppendText(@"D:\temp\new.txt");
SW.WriteLine("This line is Appended");
SW.Close();
Console.WriteLine("Appended OK");
}
删除存在的文件.. 并清空listbox 内容:
private void button4_Click(object sender, System.EventArgs e)
{
File.Delete(@"D:\temp\new.txt");
listBox1.Items.Clear();
}
另: 以上都是对固定路径进行操作.. 有很多时候需要用户自己选择文件 跳出对话框选择文件:
OpenFileDialog f = new OpenFileDialog();
剩余5页未读,继续阅读
资源评论
越过越咸
- 粉丝: 35
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功