ASP. NET read TXT file (Notepad) content:
using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
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.IO; Get TXT file stream namespace test {public partial class Text:System.Web.UI.Page {protected void Page_Load (object send
Er, EventArgs e) {Response.Write (Getinteridlist ("Asp.txt"));
//Read TXT file content public string getinteridlist (String strfile) {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;
return strout; }
}
}
Read TXT file content is to get the file stream, remember to reference using System.IO;.
Asp. NET Write TXT file (Notepad):
String txtpath = Server.MapPath ("~\\public\\attinfo\\") + "Test.txt";
StreamWriter sw = new StreamWriter (Txtpath, False, System.Text.Encoding.Default);
Sw. WriteLine ("Hello World");
Sw. WriteLine (""); Output empty line
SW. WriteLine ("ASP. NET programming-cloud-dwelling community! ");
Sw. Close ();
Note: If write Notepad does not need to wrap, you can use write, you need to wrap, you can use WriteLine.
Asp. NET Save txt file (Notepad):
public void ProcessRequest (HttpContext context)
{context
. Response.Clear ();
Context. Response.Buffer = true;
Server.URLEncode prevents saved file names from garbled context
. Response.AddHeader ("Content-disposition", "attachment;filename=" + context. Server.URLEncode ("Consumption detail" + string.) Format ("{0:YYYYMMDDHHMMSS}", System.DateTime.Now) + ". txt");
Context. Response.ContentType = "Text/plain";
String message = "Hello World";
If the exported file is to be wrapped, use environment.newline message
= + "Hello World" + Environment.NewLine;
Context. Response.Write (message);
Stops the execution context of the page
. Response.End ();
}
Note 3 points:
1. Save file name garbled problem: using Server.URLEncode encoding
Line wrapping in 2.txt file: Environment.NewLine
3. Calls can be js:window.location.href= "Download.ashx" or window.open ("Download.ashx")
The above is about TXT file related operation, if my article is helpful to you, just order a praise.