ASP. NET read and write TXT file method (instance)!

Source: Internet
Author: User

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)!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.