1.将下边这段代码在vs中运行,右键打开bin路径,里面有生成.exe的控制台程序,然后对这个.exe配置环境变量,这样就可以用批处理修改文本中指定字符啦,如url,channel,channelid,版本号等,赶快试试吧!
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace replace
{
class Program
{
static void Main(string[] args)
{
//args是命令行参数
if (args.Length != 3)
{
Console.WriteLine("请输入文件路径 替换内容 新的内容,按照空格隔开");
//Console.WriteLine("请输入正确的替换要求");
return;
}
string path = args[0];
string oldValue = args[1];
string newValue = args[2];
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string con = sr.ReadToEnd();
con = con.Replace(oldValue, newValue);
sr.Close();
fs.Close();
FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
//清除文本中所有文件,为了防止有多余空格,或者多出多余字符
fs2.Seek(0, SeekOrigin.Begin);
fs2.SetLength(0);
StreamWriter sw = new StreamWriter(fs2);
sw.WriteLine(con);
sw.Close();
fs2.Close();
}
catch (Exception)
{
Console.WriteLine("请指定正确的路径");
}
}
}
}