如何更改指定的行

本文关键字:何更改 | 更新日期: 2023-09-27 18:12:56

我有下一个代码:

 private void Install_Click(object sender, EventArgs e)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
            var link = File.ReadLines(path + "config.ini").ToArray();
            var sb = new StringBuilder();
            foreach (var txt in link)
            {
                if (txt.Contains("PLP="))
                {
                    var PLPPath = txt.Split('=')[1];
                    sb.AppendLine(string.Format("fileInstallationKey = {0}", PLPPath));
                }
                else if (txt.Contains("Output="))
                {
                    var outputPath = txt.Split('=')[1];
                    sb.AppendLine(string.Format("outlog= {0}", outputPath));
                }
                else if (txt.Contains("Licfile="))
                {
                    var LicFilePath = txt.Split('=')[1];
                    sb.AppendLine(string.Format("license_path= {0}", LicFilePath));
                }
 }
            File.WriteAllText(path + "installer.ini", sb.ToString());

        }

我想在Installer.ini中替换这一行sb.AppendLine(string.Format("fileInstallationKey = {0}", PLPPath));

sb.AppendLine(string.Format("outlog= {0}", outputPath));
sb.AppendLine(string.Format("license_path= {0}", LicFilePath));

其他行不应更改。我怎么能做到??我用下面的代码重写了文件installer.ini,我不想要这个

如何更改指定的行

不太确定,因为这是一个ini &这不是一个纯文本文件,但应该可以工作:

string configfilename=path +"config.ini";
string installerfilename=path +"installer.ini";
string configtext = File.ReadAllText(configfilename);
string installertext = File.ReadAllText(installerfilename);
if(configtext.Contains("PLP") )
    installertext = installertext.Replace("PLP", "fileInstallationKey")
if(configtext.Contains("Output") )
    installertext = installertext.Replace("Output","outlog")
if(configtext.Contains("Licfile") )
    installertext = installertext.Replace("Licfile","license_path");
File.WriteAllText(installerfilename, installertext);