1.將下邊這段代碼在vs中運(yùn)行,右鍵打開bin路徑,里面有生成.exe的控制臺(tái)程序,然后對(duì)這個(gè).exe配置環(huán)境變量,這樣就可以用批處理修改文本中指定字符啦,如url,channel,channelid,版本號(hào)等,趕快試試吧!
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是命令行參數(shù)
? ? ? ? ? ? if (args.Length != 3)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入文件路徑 替換內(nèi)容 新的內(nèi)容,按照空格隔開");
? ? ? ? ? ? ? ? //Console.WriteLine("請(qǐng)輸入正確的替換要求");
? ? ? ? ? ? ? ? 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("請(qǐng)指定正確的路徑");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}