這篇文章主要介紹“ASP.NET對(duì)txt文件相關(guān)操作方法”,在日常操作中,相信很多人在ASP.NET對(duì)txt文件相關(guān)操作方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”ASP.NET對(duì)txt文件相關(guān)操作方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬主機(jī)、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、平利網(wǎng)站維護(hù)、網(wǎ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寫(xiě)入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ò)編程 - 創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,!"); sw.Close();
注意:如果寫(xiě)入記事本不需換行,可以使用 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)于“ASP.NET對(duì)txt文件相關(guān)操作方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
分享題目:ASP.NET對(duì)txt文件相關(guān)操作方法-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://vcdvsql.cn/article4/csshie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、建站公司、網(wǎng)站改版、用戶(hù)體驗(yàn)、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容