
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) ; } }}
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年03月10日 02时44分42秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
用float/double作为中转类型的“雷区”
2019-03-03
golang中interface的一些语法缺陷的改进
2019-03-03
vue-router路由 学习笔记
2019-03-03
数据结构与算法之栈
2019-03-03
【数据库】第七章课后题
2019-03-03
第四章 串、数组和广义表 —— BF算法和KMP算法
2019-03-03
[选拔赛1]花园(矩阵快速幂),JM的月亮神树(最短路),保护出题人(斜率优化)
2019-03-03
DLA:一种深度网络特征融合方法
2019-03-03
leetcode114(二叉树展开为链表)
2019-03-03
java —— static 关键字
2019-03-03
在 Python 调试过程中设置不中断的断点 | Linux 中国
2019-03-03
使用开源可视化工具来理解你的 Python 代码 | Linux 中国
2019-03-03
【2021 ECUG Con】聚势而来,与你相约花开时
2019-03-03
硬核观察 | 有人在比特币骗局中损失了 10 个比特币
2019-03-03
FreeDOS 的简单介绍 | Linux 中国
2019-03-03
使用 top 命令了解 Fedora 的内存使用情况 | Linux 中国
2019-03-03
Linux 上最好的五款音乐播放器 | Linux 中国
2019-03-03
传输层协议
2019-03-03
细数哪些网络用户需要换IP?
2019-03-03