C# 读写文件----修改配置文件StreamWriter streamw = new StreamWriter(fs)

1988_1989 2013-02-19 04:59:04
配置文件中:
a=1
b=2
修改配置:结果如下:
a=3
b=4

方法是这样写的:
String ReadTxt,WriteTxt;
WriteTxt= String.Empty;
//if (!File.Exists(path))
//{
// using (StreamWriter streamwc = File.CreateText(path))
// {
// streamwc.Close();
// }
//}
//FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
//StreamWriter sw = new StreamWriter(path,false);

using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
{
ReadTxt = sr.ReadToEnd();
}
String[] FileRead = System.Text.RegularExpressions.Regex.Split(ReadTxt , "\r\n");
foreach(String ReadLine in FileRead)
{
if (ReadLine.Contains("="))
{
string subStr = ReadLine.Substring(0, ReadLine.LastIndexOf('='));
string keyStr = Key ;
if (subStr.Equals(keyStr))
{
WriteTxt += ReadLine.Substring(0, ReadLine.LastIndexOf('=')) +"="+value;
FileStream fs = new System.IO.FileStream(path,FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);

using (StreamWriter streamw = new StreamWriter(fs))
{
streamw.WriteLine( WriteTxt);
fs.Close();
streamw.Flush();
streamw.Close();
}

//sw.Write(WriteTxt);
//sw.Dispose();
}
}
}



在红色部分处,出现异常:流不可写。


...全文
295 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
1988_1989 2013-02-22
  • 打赏
  • 举报
回复
已解决 哈哈哈哈哈哈哈
1988_1989 2013-02-19
  • 打赏
  • 举报
回复
using (StreamWriter streamw = new StreamWriter(fs)) 这句话出现异常:流不可写入

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧