ASP. net for txt file-related operations (read, write, save), asp. nettxt
ASP. NET reads the 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; // obtain the namespace test {public partial class Text: System. web. UI. page {protected void Page_Load (object sender, Event Args e) {Response. write (GetInterIDList ("asp.txt");} // read the 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 ;}}}
Reading the txt file is to get the file stream. Remember to reference using System. IO ;.
ASP. NET ):
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 row sw. writeLine ("ASP. NET network programming-helping customers! "); Sw. Close ();
Note: If you do not need to wrap the text in notepad, you can use Write. If you need to wrap the text, you can use WriteLine.
ASP. NET Save the txt file (Notepad ):
Public void ProcessRequest (HttpContext context) {context. response. clear (); context. response. buffer = true; // Server. urlEncode prevents garbled context of the saved file name. response. addHeader ("Content-Disposition", "attachment; filename =" + context. server. urlEncode ("Consumption details" + string. format ("{0: yyyyMMddHHmmss}", System. dateTime. now) + ". txt "); context. response. contentType = "text/plain"; string message = "Hello World"; // use Environment to wrap the exported file. newLine message + = "Hello World" + Environment. newLine; context. response. write (message); // stop the execution context on the page. response. end ();}
NOTE 3:
1. Saving the file name garbled: Use Server. UrlEncode for encoding
2.txt: Environment. NewLine
3. js: window. location. href = "download. ashx" or window. open ("download. ashx ")
The above is aboutTxt fileIf my article is helpful to you, just give it a compliment.