ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)
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);
//停止頁面的執(zhí)行
context.Response.End();
}
注意3點(diǎn):
1.保存文件名亂碼問題:用Server.UrlEncode編碼
2.txt文件中的換行問題:Environment.NewLine
3.調(diào)用可以用js:window.location.href="download.ashx" 或window.open("download.ashx")
以上就是關(guān)于txt文件的相關(guān)操作,如果我的文章對(duì)你有幫助,就點(diǎn)個(gè)贊吧。
相關(guān)文章
ASP.NET web.config中 數(shù)據(jù)庫連接字符串加密解密
本文主要介紹利用aspnet_regiis.exe工具對(duì)web.config中connectionStrings節(jié)點(diǎn)進(jìn)行加密和解密的過程,希望對(duì)大家有所幫助。2016-05-05
Visual Studio 2015 配置 Opencv3.2的圖文詳解
這篇文章主要介紹了Visual Studio 2015 配置 Opencv3.2的圖文詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Asp.Net MVC3.0如何項(xiàng)目部署到Win7 64位系統(tǒng)
這篇文章主要介紹了Asp.Net MVC3.0如何項(xiàng)目部署到Win7 64位系統(tǒng)的全部過程,需要的朋友可以參考下2015-10-10
asp.net顯示自己的網(wǎng)頁圖標(biāo)的幾種方式
多tab的瀏覽器,你一定會(huì)發(fā)現(xiàn)tab前邊的個(gè)性圖標(biāo),關(guān)于這個(gè)東西有好幾種做法,下面與大家分享下2014-05-05
WCF中使用nettcp協(xié)議進(jìn)行通訊的方法
這篇文章主要給大家介紹了關(guān)于WCF中使用nettcp協(xié)議進(jìn)行通訊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用WCF具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
ASP.NET使用SignalR2實(shí)現(xiàn)服務(wù)器廣播
這篇文章介紹了ASP.NET使用SignalR2實(shí)現(xiàn)服務(wù)器廣播的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
aspx中的mysql操作類sqldatasource使用示例分享
服務(wù)器裝了mysql odbc驅(qū)動(dòng),想在那個(gè)iis上操作另一個(gè)服務(wù)器的mysql,找到個(gè).net的sqldatasource類可以操作mysql,下在把使用方法分享一下2014-01-01

