c#怎么替换文本中指定内容?
1个回答
展开全部
//经过我认真的测试下面代码没有问题,你再试试。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
string path = @"config.txt";
string con = "";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
con = sr.ReadToEnd();
con=con.Replace("127.0.0.1", "192.168.1.1");
sr.Close();
fs.Close();
FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs2);
sw.WriteLine(con);
sw.Close();
fs2.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
string path = @"config.txt";
string con = "";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
con = sr.ReadToEnd();
con=con.Replace("127.0.0.1", "192.168.1.1");
sr.Close();
fs.Close();
FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs2);
sw.WriteLine(con);
sw.Close();
fs2.Close();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询