runxinzhi.com
首页
百度搜索
.NET中的文件IO操作实例
从TextBox控件中写入到txt文本
Code
//
从testbox中写入到txt文本
protected
void
Button5_Click(
object
sender, EventArgs e)
{
string
text
=
txtContent.Text;
if
(
!
string
.IsNullOrEmpty(text))
{
//
指定文件的完整路径
string
fileName
=
Server.MapPath(
"
~/txt/test.txt
"
);
//
判断该文件是否存在
if
(File.Exists(fileName))
{
//
如果存在,就先删掉
File.Delete(fileName);
}
else
{
//
创建一个文件操作的流
FileStream stream
=
new
FileStream(fileName, FileMode.Create);
//
创建一个写操作流
StreamWriter writer
=
new
StreamWriter(stream, Encoding.UTF8);
//
进行写操作
writer.Write(text);
//清空控件中的文字
txtContent.Text
=
string
.Empty;
//
关闭流,不然出现异常
writer.Close();
stream.Close();
}
}
else
{
Response.Write(
"
<script>alert(\
"
空的列!\
"
)</script>
"
);
}
}
然后再从生成的test.txt中读取数据,显示到TextBox控件中(方法同理)
Code
//
读取文本到textbox中显示
protected
void
Button6_Click(
object
sender, EventArgs e)
{
string
fileName
=
Server.MapPath(
"
~/txt/test.txt
"
);
if
(File.Exists(fileName))
{
FileStream stream
=
new
FileStream(fileName, FileMode.Open);
StreamReader reader
=
new
StreamReader(stream, Encoding.UTF8);
txtContent.Text
=
reader.ReadToEnd();
reader.Close();
stream.Close();
}
else
{
Response.Write(
"
<script>alert(\
"
没有test.txt文件!\
"
)</script>
"
);
}
}
相关阅读:
PyTools包罗万象的python工具包
手撕神经网络实验报告
数组模拟队列 以及队列的复用(环形队列)
Sentinel熔断降级
二维数组与稀疏数组的转换dataStructures
Sentine熔断降级进阶
Centos7防火墙以及端口控制
docker的安装以及使用命令
ToDesk个人免费 极致流畅的远程协助软件
typora+PicGo+gitee搭建免费的的床
原文地址:https://www.cnblogs.com/kingfly/p/1567503.html
最新文章
MySQL学习笔记(七)Index Merge
Unity与Android通信
KeyStore
Unity ref out
markdown语法
IDEA创建包的重叠问题
DOS命令
使用腾讯云函数实现网易云音乐自动签到
使用宝塔面板配合可道云打造私有云
自翻Office 2013 RT 使用说明
热门文章
如何在win10的桌面上加入uwp应用的快捷方式
termux旧手机变废为宝(下)
shell编程入门
termux 进阶
袁爷爷一路走好
超简单笔记本改造nas一个萌新的摸爬滚打
祝大家元旦快乐
linux下查看当前目录下所有文件的大小
【备份】【D1 Lichee RV & WIFI】RTL8723DS & BS 的 WiFi 移植记录
【2021】在 Sipeed 做开源事业の工作总结
Copyright © 2020-2023
润新知