Asp.net读取和写入txt文件方法(实例)!
====================================================== 【程序第一行的引入命名空间文件 - 参考】
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
====================================================== 【读取 - 参考网上的,根据需求改动了一下】
string strfile;
strfile = "asp.txt";
string strout;
strout = "";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(strfile)))
{
}
else
{
StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(strfile), System.Text.Encoding.Default);
String input = sr.ReadToEnd();
sr.Close();
strout = input;
}
Response.Write("ssss");
// =========================================== 【读取的另一种写法】
using system.test;
string txt = File.ReadAllText("d:\\aaa.sys", Encoding.Default);
Response.Write("【" + txt + "】");
====================================================== 【写入 - 参考】
System.IO.File.WriteAllText("e:\\asp_1.txt", "I LOVE YOU!wang na"); // 一定要绝对路径