zoukankan
html css js c++ java
.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>
"
);
}
}
查看全文
相关阅读:
WCF 绑定(Binding)
WCF 配置服务 (02)
WCF 双工模式
.NET开源高性能Socket通信中间件Helios介绍及演示
关于WCF服务在高并发情况下报目标积极拒绝的异常处理
HTTP状态管理机制之Cookie
JavaScript 总结几个提高性能知识点
windows下nginx安装、配置与使用
Windows下Nginx的安装与配置
大型架构.net平台篇(WEB层均衡负载nginx)
原文地址:https://www.cnblogs.com/kingfly/p/1567503.html
最新文章
First,FirstOrDefault,Single,SingleOrDefault的区别
详解C#break ,continue, return
DICOM医学图像处理:DIMSE消息发送与接收“大同小异”之DCMTK fo-dicom mDCM
MetadataType的使用,MVC的Model层数据验证
Asp.Net MVC 模型(使用Entity Framework创建模型类)
C#综合揭秘——Entity Framework 并发处理详解
Entity Framework 增删改查和事务操作
Entity Framework 教程
EF框架step by step(9)—Code First Fluent API
EF框架step by step(8)—Code First DataAnnotations(2)
热门文章
EF框架step by step(7)—Code First DataAnnotations(1)
EF框架step by step(6)—处理实体complex属性
EF框架step by step(5)—处理实体简单属性
EF框架step by step(4)—DBcontext应用于已存在数据库
EF框架step by step(3)—Code-First
EF框架step by step(2)—Model-First
EF框架step by step(1)—Database-First
.NET程序的性能要领和优化建议
.Net 垃圾回收机制原理(二)
.Net 垃圾回收机制原理(一)
Copyright © 2011-2022 走看看