我有下一个代码:
private void Install_Click(object sender, EventArgs e)
{
string configfilename = path + "config.ini";
string installerfilename = path + "installer.ini";
string configtext = File.ReadAllText(configfilename);
string installertext = File.ReadAllText(installerfilename);
var link = File.ReadLines(path + "config.ini").ToArray();
var lin = File.ReadLines(path + "installer.ini").ToArray();
foreach (var txt in link)
{
if (txt.Contains("PLP="))
{
var PLPPath = txt.Split('=')[1];
installertext = installertext.Replace("fileInstallationKey=", "fileInstallationKey=" + PLPPath);
}
else if (txt.Contains("Output="))
{
var outputPath = txt.Split('=')[1];
installertext = installertext.Replace("outlog=", "outlog=" + outputPath);
}
else
{
var license = lin.Select(line => Regex.Replace(line, @"license=.*", "license=yes"));
File.WriteAllLines(installerfilename, license);
var lmgr_files = lin.Select(line => Regex.Replace(line, @"lmgr_files=.*", "lmgr_files=false"));
File.WriteAllLines(installerfilename, lmgr_files);
var lmgr_service = lin.Select(line => Regex.Replace(line, @"lmgr_service=.*", "lmgr_service=false"));
File.WriteAllLines(installerfilename, lmgr_service);
}
File.WriteAllText(installerfilename, installertext);
}
但问题是此代码会复制config.ini
中来自installer.ini
,PLP row
行的Output
数据。在installer.ini
行中license
,lmgr_files
,lmgr_service
未完成数据:yes
,false
,false
。
换句话说,谁在其他人之后并没有工作。
我如何修改代码以使所有语句都执行?
在installer.ini
我有很多东西,但最重要的是:
license=false //I want to have license=yes
lmgr_files= //I want to have lmgr_files=
lmgr_service=
fileInstallationKey=0000000 //the number from config.ini
outlog="jhaoif" //the code from config.ini
答案 0 :(得分:1)
有一种读取/写入ini文件的机制。这里没有必要重新发明轮子。看一眼 Reading/writing an INI file
答案 1 :(得分:1)
试试这段代码。
string configfilename = path + "config.ini";
string installerfilename = path + "installer.ini";
var link = File.ReadLines(path + "config.ini").ToArray();
var lin = File.ReadLines(path + "installer.ini").ToArray();
IEnumerable<string> newInstaller = lin;
foreach (var txt in link)
{
if (txt.Contains("PLP="))
{
var PLPPath = txt.Split('=')[1];
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"fileInstallationKey=.*", "fileInstallationKey=" + PLPPath));
}
if (txt.Contains("Output="))
{
var outputPath = txt.Split('=')[1];
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"outlog=.*", "outlog=" + outputPath));
}
}
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"license=.*", "license=yes"));
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"lmgr_files=.*", "lmgr_files=false"));
newInstaller = newInstaller.Select(line => Regex.Replace(line, @"lmgr_service=.*", "lmgr_service=true"));
string strWrite = string.Join(Environment.NewLine, newInstaller.ToArray());
File.WriteAllText(installerfilename,strWrite);