最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net c写文件函数实例代码
时间:2010-09-15 编辑:简简单单 来源:一聚教程网
asp教程.net c写文件函数实例代码
streamwriter 和 streamreader 向流写入字符并从流读取字符。下面的代码示例打开 log.txt 文件(如果文件不存在则创建文件)以进行输入,并将信息附加到文件尾。然后将文件的内容写入标准输出,以便显示出来。
[c#]
using system;
using system.io;
class dirappend
{
public static void main(string[] args)
{
streamwriter w = file.appendtext("log.txt");
log ("test1", w);
log ("test2", w);
// close the writer and underlying file.
w.close();
// open and read the file.
streamreader r = file.opentext("log.txt");
dumplog (r);
}public static void log (string logmessage, textwriter w)
{
w.write("rnlog entry : ");
w.writeline("{0} {1}", datetime.now.tolongtimestring(),
datetime.now.tolongdatestring());
w.writeline(" :");
w.writeline(" :{0}", logmessage);
w.writeline ("-------------------------------");
// update the underlying file.
w.flush();
}public static void dumplog (streamreader r)
{
// while not at the end of the file, read and write lines.
string line;
while ((line=r.readline())!=null)
{
console.writeline(line);
}
r.close();
}
}
读取文件二
using (filestream fs = new filestream(file, filemode.open,fileaccess.readwrite))
{
xmldocument toxml = new xmldocument();
toxml.load(fs);
//do some modification for the xml.
fs.flush();
toxml.save(fs);
}
更多详细内容请查看:http://www.111com.net/net/c/33608.htm
读取文件三
写文件
public static void writefile(string filepath, string str)
{
streamwriter sr;
if (file.exists(filepath)) //如果文件存在,则创建file.appendtext对象
{
sr = file.appendtext(filepath);
}
else //如果文件不存在,则创建file.createtext对象
{
sr = file.createtext(filepath);
}
sr.writeline(str);
sr.close();
}
-
下一个: asp.net验证码代码(无刷新验证码)
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10