c# 中操作文本

本文介绍了使用C#进行文件的创建与读取操作的方法。包括如何创建文本文件并写入内容,以及如何从指定路径读取文件内容到字符串变量中。

创建文本:
string path;
path=@Application.StartupPath+"//log//log"+DateTime.Now.ToString("yyyy-MM--dd hhmmss")+".txt";
StreamWriter sw=File.CreateText(path);
sw.Write("hello");
sw.WriteLine("hello");
sw.Close();
­
读取文本
string str;
string address="";
FileStream fs;
string path;
path=Application.StartupPath+"//log//"+"1.txt";
try
{
fs=new FileStream(path,FileMode.OpenOrCreate);
}
catch(FileNotFoundException ex)
{
MessageBox.Show("读取文件出现错误!"+ex.Message,"错误",0x00);
return;
}
StreamReader sr=new StreamReader(fs);
while((str=sr.ReadLine())!=null)
{
address=address+str;
}
sr.Close();
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值