会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
王翔 KingFly PHP
WEB开发者的技术乐园 千人技术群:306157454
首页
联系
管理
.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>
"
);
}
}
posted @
2009-09-16 10:22
王翔(kingfly)
阅读(
414
) 评论(
3
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部