asp.net 4.5 练习~test14-5 写文件
发布日期:2021-05-06 21:16:23 浏览次数:11 分类:技术文章

本文共 2211 字,大约阅读时间需要 7 分钟。

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test14_5.WebForm1" %>

webform1.aspx.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace test14_5{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            string fileName = System.IO.Path.Combine(Request.PhysicalApplicationPath, @"test.txt");            if (System.IO.File.Exists(fileName))            {                Label1.Text = readText();            }            else            {                string s = "The First Line!";                appendText(s);                Label1.Text = s;            }        }        protected void Button1_Click(object sender, EventArgs e)        {            string str1 = TextBox1.Text.Trim();            if (str1.Length > 0)            {                appendText(str1);                Label1.Text = readText();            }        }        //写文件        private void appendText(string addText)        {            string fileName = System.IO.Path.Combine(Request.PhysicalApplicationPath, @"test.txt");            System.IO.FileStream streamWrite = System.IO.File.Open(fileName, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);            byte[] data1 = System.Text.Encoding.ASCII.GetBytes(addText);            streamWrite.Write(data1, 0, data1.Length);            streamWrite.Flush();            streamWrite.Close();        }        //读文件        private string readText()        {            string fileName = System.IO.Path.Combine(Request.PhysicalApplicationPath, @"test.txt");            System.IO.FileStream streamReader = System.IO.File.Open(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);            byte[] data2 = new byte[streamReader.Length];            streamReader.Read(data2, 0, (int)streamReader.Length);            streamReader.Close();            return System.Text.Encoding.ASCII.GetString(data2) ;        }    }}

 

上一篇:asp.net 4.5 练习~test14-6 streamwrite写文件
下一篇:asp.net 4.5 练习~test14-4 移动复制文件

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年03月10日 02时44分42秒