txt文件读取和写入(.net)
2007-08-02 16:36
351 查看
//检查文件,如果文件不存在则创建
private void ExistsFile(string FilePath)
{
if(!File.Exists(FilePath))
File.Create(FilePath);
}
private void Button2_Click(object sender, System.EventArgs e)
{
ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在
//读取文件
StreamReader sr = new StreamReader(Server.MapPath("test/weather.txt"), System.Text.Encoding.Default);
try
{
string input = sr.ReadToEnd();
sr.Close();
input = input.Replace("/r/n", "").Replace("/n", "");
this.TextBox1.Text = input;
}
catch
{
Response.Write("<script>alert('文件读取失败');</script>");
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在
//写入文件
StreamWriter sr = new StreamWriter(Server.MapPath("test/weather.txt"),false,System.Text.Encoding.Default);
try
{
sr.Write(this.TextBox1.Text);
sr.Close();
Response.Write("<script>alert('文件写入成功');</script>");
}
catch
{
Response.Write("<script>alert('文件写入失败');</script>");
}
}
private void ExistsFile(string FilePath)
{
if(!File.Exists(FilePath))
File.Create(FilePath);
}
private void Button2_Click(object sender, System.EventArgs e)
{
ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在
//读取文件
StreamReader sr = new StreamReader(Server.MapPath("test/weather.txt"), System.Text.Encoding.Default);
try
{
string input = sr.ReadToEnd();
sr.Close();
input = input.Replace("/r/n", "").Replace("/n", "");
this.TextBox1.Text = input;
}
catch
{
Response.Write("<script>alert('文件读取失败');</script>");
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
ExistsFile(Server.MapPath("test/weather.txt"));//检查文件是否存在
//写入文件
StreamWriter sr = new StreamWriter(Server.MapPath("test/weather.txt"),false,System.Text.Encoding.Default);
try
{
sr.Write(this.TextBox1.Text);
sr.Close();
Response.Write("<script>alert('文件写入成功');</script>");
}
catch
{
Response.Write("<script>alert('文件写入失败');</script>");
}
}
相关文章推荐
- asp.net读取和写入txt文件方法
- c#asp.net 读取 写入 txt 文件 asp.net操作文件
- asp.net(c#)如何读取上传过程中的.txt文件中的数据,并将其写入数据库的
- Asp.net读取和写入txt文件方法(实例)!
- VB.NET 读取INI,TXT文件 写入数据
- ASP.NET对txt文件相关操作(读取、写入、保存)
- ASP.NET对txt文件相关操作(读取、写入、保存)
- c#asp.net 读取 写入 txt 文件 asp.net操作文件
- Asp.net读取和写入txt文件方法
- Asp.net读取和写入txt文件方法(实例)!
- c#asp.net 读取 写入 txt 文件 asp.net操作文件
- script.net写入txt和读取txt文件示例
- matlab txt文件按行打乱顺序 txt按行读取 按行写入 打乱顺序
- [导入]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- MFC读取TXT文件,写入编辑框
- nodejs 读取JSON文件写入txt中 fs.readFile&fs.writeFile
- VS.net配合VSS时,编译报错:未能向文件“.csproj.FileListAbsolute.txt”写入命令行 对路径 的访问被拒绝。
- Java读取txt文件和写入txt文件
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- c++学习笔记—c++对txt文件的读取与写入