濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)

ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)

熱門標(biāo)簽:南昌自動(dòng)外呼系統(tǒng)線路 聊城智能電銷機(jī)器人電話 海東防封電銷卡 云南外呼系統(tǒng)代理 安陸市地圖標(biāo)注app 寧德防封版電銷卡 辦公用地圖標(biāo)注網(wǎng)點(diǎn)怎么操作 上海市三維地圖標(biāo)注 西寧電銷外呼系統(tǒng)公司

ASP.NET讀取txt文件(記事本)內(nèi)容:

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; 
//獲取txt文件流 
namespace test 
{ 
 public partial class Text : System.Web.UI.Page 
 { 
  protected void Page_Load(object sender, EventArgs e) 
  { 
   Response.Write(GetInterIDList("asp.txt")); 
  } 
  //讀取txt文件的內(nèi)容 
  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; 
  } 
 } 
}

讀取txt文件內(nèi)容就是獲取文件流,記得要引用using System.IO;。

ASP.NET寫入txt文件(記事本):

string txtPath = Server.MapPath("~\\Public\\AttInfo\\") + "Test.txt"; 
StreamWriter sw = new StreamWriter(txtPath, false, System.Text.Encoding.Default); 
sw.WriteLine("Hello World"); 
sw.WriteLine(""); //輸出空行 
sw.WriteLine("ASP.NET網(wǎng)絡(luò)編程 - 腳本之家!"); 
sw.Close();

注意:如果寫入記事本不需換行,可以使用 Write,需要換行的,可以使用 WriteLine。

ASP.NET保存txt文件(記事本):

public void ProcessRequest(HttpContext context)
  {

   context.Response.Clear();
   context.Response.Buffer = true;
   //Server.UrlEncode 防止保存的文件名亂碼
   context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("消費(fèi)明細(xì)" + string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now) + ".txt")); 
   context.Response.ContentType = "text/plain"; 
   string message = "Hello World";
   //如果導(dǎo)出的文件要換行,用Environment.NewLine
   message += "Hello World" + Environment.NewLine;
   context.Response.Write(message);
   //停止頁(yè)面的執(zhí)行  
   context.Response.End(); 
  }

注意3點(diǎn):

1.保存文件名亂碼問(wèn)題:用Server.UrlEncode編碼

2.txt文件中的換行問(wèn)題:Environment.NewLine

3.調(diào)用可以用js:window.location.href="download.ashx" 或window.open("download.ashx")

以上就是關(guān)于txt文件的相關(guān)操作,如果我的文章對(duì)你有幫助,就點(diǎn)個(gè)贊吧。

您可能感興趣的文章:
  • ASP.Net動(dòng)態(tài)讀取Excel文件最簡(jiǎn)方法
  • 如何在ASP.NET Core類庫(kù)項(xiàng)目中讀取配置文件詳解
  • asp.net讀取模版并寫入文本文件
  • ASP.NET中上傳并讀取Excel文件數(shù)據(jù)示例
  • asp.net讀取excel文件的三種方法示例
  • asp.net讀取磁盤文件、刪除實(shí)例代碼
  • ASP.NET(C#)讀取Excel的文件內(nèi)容
  • asp.net 讀取配置文件方法
  • C#/.NET讀取或修改文件的創(chuàng)建時(shí)間及修改時(shí)間詳解

標(biāo)簽:贛州 汕尾 南寧 崇左 衢州 洛陽(yáng) 青海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)》,本文關(guān)鍵詞  ASP.NET,對(duì),txt,文件,相關(guān),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    资兴市| 沅陵县| 英吉沙县| 古交市| 太保市| 通榆县| 托克托县| 油尖旺区| 旬邑县| 镇原县| 湛江市| 中方县| 江阴市| 普格县| 云安县| 翁牛特旗| 民丰县| 枞阳县| 勐海县| 苏尼特左旗| 尼勒克县| 招远市| 白玉县| 和静县| 铜梁县| 淮滨县| 舟曲县| 台北市| 隆安县| 兴山县| 白城市| 固阳县| 南通市| 红河县| 松滋市| 如东县| 乐至县| 卢龙县| 东至县| 平谷区| 利辛县|