zoukankan
html css js c++ java
c#读取/写入文件
Code
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.IO;
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
/**/
///
<summary>
///
检查文件是否存在
///
</summary>
///
<param name="FilePath"></param>
private
void
ExistsFile(
string
FilePath)
{
if
(
!
File.Exists(FilePath))
{
File.Create(FilePath);
}
}
//
读取文件
protected
void
Button2_Click(
object
sender, EventArgs e)
{
ExistsFile(Server.MapPath(
"
~/test/111.txt
"
));
//
读取文件
StreamReader sr
=
new
StreamReader(Server.MapPath(
"
~/test/111.txt
"
), System.Text.Encoding.Default);
try
{
string
input
=
sr.ReadToEnd();
sr.Close();
input
=
input.Replace(
"
<br>
"
,
"
\r\n
"
);
Response.Write(
"
<b>文件读取成功!</b>
"
);
TextBox1.Text
=
input;
}
catch
{
Response.Write(
"
<b>文件读取失败!</b>
"
);
}
}
//
写入文件
protected
void
Button1_Click(
object
sender, EventArgs e)
{
ExistsFile(Server.MapPath(
"
~/test/111.txt
"
));
//
写入文件
StreamWriter sw
=
new
StreamWriter(Server.MapPath(
"
~/test/111.txt
"
),
false
, System.Text.Encoding.Default);
try
{
sw.Write(TextBox1.Text);
sw.Close();
Response.Write(
"
<b>文件写入成功!</b>
"
);
}
catch
{
Response.Write(
"
<b>文件写入失败!</b>
"
);
}
}
}
查看全文
相关阅读:
【Python应用:基于PyQt5文本识别】调用百度AI对一张或多张图片、文件夹中的图片和屏幕区域截图进行识别(PDF转Word 小意思)
【Ubuntu搭建Django编程环境】:创建python虚拟开发环境和配置pip国内镜像源
23种设计模式上篇
荷兰国旗问题
文件复制多份
mybatis批量更新
数组小和
常见排序算法
福尔摩斯的约会
小明上学
原文地址:https://www.cnblogs.com/kingfly/p/1584209.html
最新文章
网络的瓶颈效应
编程语言分类
BUG一家亲
虚拟环境的搭建
DRF框架之三大认证、异常组件
drf框架之接口、认证
drf框架之小总结
DRF框架之视图集、认证规则
DRF框架之十大接口、视图家族
DRF框架之Response二次封装、数据库优化、序列化
热门文章
DRF框架之反序列化校验
DRF框架之请求生命周期、序列化组件
Linux vi/vim使用入门
【Python应用】爬取有道API,自制工具(Python爬虫:反爬破解)
Ubuntu18.04的wine安装和使用详解(解决中文乱码方框)
windows使用技巧
【Linux】Ubuntu18.04(其他Linux系统)安装deepin的微信和TIM脚本--Install.sh
【python学习笔记:Django】5.向服务器传递参数
【python学习笔记:Django】4.JsonResponse返回中文字典编码错误
git for windows 下载速度慢?
Copyright © 2011-2022 走看看