C#对文件进行读写
时间:2010-06-08 来源:lijun2087
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string time = System.DateTime.Now.ToShortTimeString();
string text = textBox1.Text;
string sss = System.DateTime.Now.ToShortDateString() + ".txt";
sss = "d:\\" + sss;
File.Delete(sss);
FileStream fst = new FileStream(sss, FileMode.Append);
//写数据到a.txt格式
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(time + '\r' + "写" + text);
swt.Close();
fst.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string ss = System.DateTime.Now.ToShortDateString() + ".txt";
ss = "d:\\" + ss;
//FileStream fs = new FileStream(sss,
try
{
StreamReader srt = new StreamReader(ss, Convert.ToBoolean(FileMode.Open));
//存在
textBox2.Text = srt.ReadToEnd();
srt.Close();
}
catch (FileNotFoundException ee)
{
textBox2.Text = ee.ToString();
}
}
}
}
//删除文件 FileInfo fi=new FileInfo("a.txt");
//fi.Delete();