不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
C#读写文本文件小结
编辑:dnawo 日期:2008-05-17
除了创建、复制、移动和删除外,对文本文件最常用的操作就是进行读写,C#提供了非常多的方法来对文本文件进行读写,今天我们做个小结:
一、写入文件
1.File类的静态方法WriteAllText(改写方式)
2.File类的静态方法WriteAllLines(改写方式)
3.File类的静态方法AppendAllText(追加方式)
4.StreamWriter对象的Write方法和WriteLine方法(true追加,false改写)
说明:当文件不存在时,以上方法都将创建一个新文件!
二、读取文件
1.File类的静态方法ReadAllText
2.File类的静态方法ReadAllLines
3.StreamReader对象的ReadToEnd方法
4.StreamReader对象的ReadLine方法
一、写入文件
1.File类的静态方法WriteAllText(改写方式)
复制内容到剪贴板
程序代码

File.WriteAllText(Server.MapPath("a.txt"), "http://www.mzwu.com/\r\nhttp://www.hao123.com/\r\nhttp://www.163.com/");
2.File类的静态方法WriteAllLines(改写方式)
复制内容到剪贴板
程序代码

File.WriteAllLines(Server.MapPath("a.txt"), new string[] { "http://www.mzwu.com/", "http://www.hao123.com/", "http://www.163.com/" });
3.File类的静态方法AppendAllText(追加方式)
复制内容到剪贴板
程序代码

File.AppendAllText(Server.MapPath("a.txt"), "http://www.mzwu.com/\r\nhttp://www.hao123.com/\r\n");
File.AppendAllText(Server.MapPath("a.txt"), "http://www.163.com/");
File.AppendAllText(Server.MapPath("a.txt"), "http://www.163.com/");
4.StreamWriter对象的Write方法和WriteLine方法(true追加,false改写)
复制内容到剪贴板
程序代码

StreamWriter sw = new StreamWriter(Server.MapPath("a.txt"), false);
sw.Write("http://www.mzwu.com/\r\n");
sw.WriteLine("http://www.mzwu.com/");
sw.WriteLine("http://www.163.com/");
sw.Close();
sw.Write("http://www.mzwu.com/\r\n");
sw.WriteLine("http://www.mzwu.com/");
sw.WriteLine("http://www.163.com/");
sw.Close();
说明:当文件不存在时,以上方法都将创建一个新文件!
二、读取文件
1.File类的静态方法ReadAllText
复制内容到剪贴板
程序代码

File.ReadAllText(Server.MapPath("a.txt"))
2.File类的静态方法ReadAllLines
复制内容到剪贴板
程序代码

string[] content = File.ReadAllLines(Server.MapPath("a.txt"));
for (int i = 0; i < content.Length; i++)
{
Response.Write(content[i] + "<br/>");
}
for (int i = 0; i < content.Length; i++)
{
Response.Write(content[i] + "<br/>");
}
3.StreamReader对象的ReadToEnd方法
复制内容到剪贴板
程序代码

StreamReader sr = new StreamReader(Server.MapPath("a.txt"));
Response.Write(sr.ReadToEnd());
sr.Close();
Response.Write(sr.ReadToEnd());
sr.Close();
4.StreamReader对象的ReadLine方法
复制内容到剪贴板
程序代码

StreamReader sr = new StreamReader(Server.MapPath("a.txt"));
while (!sr.EndOfStream)
{
Response.Write(sr.ReadLine() + "<br/>");
}
sr.Close();
while (!sr.EndOfStream)
{
Response.Write(sr.ReadLine() + "<br/>");
}
sr.Close();
评论: 0 | 引用: 0 | 查看次数: 5505
发表评论
请登录后再发表评论!