[英]How I can modify the code to make work all statements?
我有下一个代码:
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);
}
但是问题是此代码从PLP row
, Output
行从installer.ini
config.ini
复制数据。 在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
有一种机制可以读取/写入ini文件。 这里不需要重新发明轮子。 看一下读取/写入INI文件
试试这个代码。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.