ASP. NET read and write TXT file method (instance)!
"Introduction of the first line of the program namespace file-Reference"
System;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.UI; usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.IO;
"Read-reference on-line, changed as needed"
strfile; Strfile="Asp.txt"; stringStrout; Strout=""; if(!file.exists (System.Web.HttpContext.Current.Server.MapPath (strfile))) { } Else{StreamReader SR=NewStreamReader (System.Web.HttpContext.Current.Server.MapPath (strfile), System.Text.Encoding.Default); String input=Sr. ReadToEnd (); Sr. Close (); Strout=input; } Response.Write ("ssss");
"Write-Reference"
System.IO.File.WriteAllText ("e:\\asp_1.txt""I love You!wang na" ); // be sure to have absolute paths
"Another way to read the wording"
system.test; string txt = file.readalltext ("d:\\aaa.sys", Encoding.default); Response.Write ( "" "" "" "
C#asp.net Read Write TXT file
Read TXT (already tested) Public voidReadData () {//C # Read TXT file establishment FileStream object, plainly tell the program,//where the file is, how to handle the file, and how to handle the contents of the fileSystem.Text.Encoding code = System.Text.Encoding.GetEncoding ("gb2312"); FileStream FS=NewFileStream (Server.MapPath ("./wxlm/huo.txt"), FileMode.Open, FileAccess.Read); //Read and write to text onlyStreamReader sr =NewStreamReader (Fs,code); //position operation Point, Begin is a reference pointSr. Basestream.seek (0, Seekorigin.begin); //read on to see if there is any content in the file to provide a basis for the next loop.//Sr. ReadLine () Here is StreamReader's main point is not in the console of ~ stringstr = Sr. ReadToEnd ();//If the file has content//C # Read the TXT file's closing file, pay attention to the order, first closed to the inside of the file, then the file ~Sr. Close (); Fs. Close (); Response.Write (str); }
ASP. NET read and write TXT file method (instance)!