C
C#2y ago
Estal

❔ Replace multiple Lines

What I had before was
C#
string SessionName = File.ReadAllText(ConfigFile);
SessionName = SessionName.Replace("SessionName=\"" + LoadSetting(ConfigFile, "SessionName") + "\"", "SessionName=\"" + serverName.Text + "\"");
File.WriteAllText(ConfigFile, SessionName);

string Password = File.ReadAllText(ConfigFile);
Password = Password.Replace("JoinPassword=\"" + LoadSetting(ConfigFile, "JoinPassword") + "\"", "JoinPassword=\"" + serverPass.Text + "\"");
File.WriteAllText(ConfigFile, Password);
C#
string SessionName = File.ReadAllText(ConfigFile);
SessionName = SessionName.Replace("SessionName=\"" + LoadSetting(ConfigFile, "SessionName") + "\"", "SessionName=\"" + serverName.Text + "\"");
File.WriteAllText(ConfigFile, SessionName);

string Password = File.ReadAllText(ConfigFile);
Password = Password.Replace("JoinPassword=\"" + LoadSetting(ConfigFile, "JoinPassword") + "\"", "JoinPassword=\"" + serverPass.Text + "\"");
File.WriteAllText(ConfigFile, Password);
I now realize that I'm essentially opening the file every iteration. What I'm trying to figure out is how I can go through multiple lines and write them all at once time. If anyone knows it'd be appreciated. Gonna keep trying to figure it out for the time being.
5 Replies
Angius
Angius2y ago
It'd be best to use JSON if possible Read -> deserialize -> update -> serialize -> save
Estal
EstalOP2y ago
Unfortunately it's not. 😦 It's an ini file for a server that the program handles. Suppose I could have it read->Serialize->Update->SaveNew->ReadNew->Deserialize->Save that just feels like a headache though lol
Angius
Angius2y ago
Why would you want to read the file you just saved? Just read it once, do all the modification you need, then save it
Estal
EstalOP2y ago
I'll give it a go. New approach for me. Thanks Okay, When you said why would you want to read the file you just saved it made me think some more. The answer was so obvious, so THANK YOU. Simply changed the data to
C#
string FullConfig = File.ReadAllText(ConfigFile);
FullConfig = FullConfig.Replace("SessionName=\"" + LoadSetting(ConfigFile, "SessionName") + "\"", "SessionName=\"" + serverName.Text + "\"");
FullConfig = FullConfig.Replace("JoinPassword=\"" + LoadSetting(ConfigFile, "JoinPassword") + "\"", "JoinPassword=\"" + serverPass.Text + "\"");
FullConfig = FullConfig.Replace("AdminPassword=\"" + LoadSetting(ConfigFile, "AdminPassword") + "\"", "AdminPassword=\"" + adminPass.Text + "\"");
FullConfig = FullConfig.Replace("MaxPlayers=" + LoadSetting(ConfigFile, "MaxPlayers"), "MaxPlayers=" + maxPlayers.Text);
FullConfig = FullConfig.Replace("ShutdownIfNotJoinedFor=" + LoadSetting(ConfigFile, "ShutdownIfNotJoinedFor"), "ShutdownIfNotJoinedFor=" + shutDownNotJoined1.Text);
FullConfig = FullConfig.Replace("ShutdownIfEmptyFor=" + LoadSetting(ConfigFile, "ShutdownIfEmptyFor"), "ShutdownIfEmptyFor=" + shutdownEmpty.Text);
FullConfig = FullConfig.Replace("ResumeProspect=" + LoadSetting(ConfigFile, "ResumeProspect"), "ResumeProspect=" + GetResumeStatus());
File.WriteAllText(ConfigFile, FullConfig);
string FinalConfig = File.ReadAllText(ConfigFile);
if (displayChanges.IsChecked == true)
{
MessageBox.Show(FinalConfig);
}
C#
string FullConfig = File.ReadAllText(ConfigFile);
FullConfig = FullConfig.Replace("SessionName=\"" + LoadSetting(ConfigFile, "SessionName") + "\"", "SessionName=\"" + serverName.Text + "\"");
FullConfig = FullConfig.Replace("JoinPassword=\"" + LoadSetting(ConfigFile, "JoinPassword") + "\"", "JoinPassword=\"" + serverPass.Text + "\"");
FullConfig = FullConfig.Replace("AdminPassword=\"" + LoadSetting(ConfigFile, "AdminPassword") + "\"", "AdminPassword=\"" + adminPass.Text + "\"");
FullConfig = FullConfig.Replace("MaxPlayers=" + LoadSetting(ConfigFile, "MaxPlayers"), "MaxPlayers=" + maxPlayers.Text);
FullConfig = FullConfig.Replace("ShutdownIfNotJoinedFor=" + LoadSetting(ConfigFile, "ShutdownIfNotJoinedFor"), "ShutdownIfNotJoinedFor=" + shutDownNotJoined1.Text);
FullConfig = FullConfig.Replace("ShutdownIfEmptyFor=" + LoadSetting(ConfigFile, "ShutdownIfEmptyFor"), "ShutdownIfEmptyFor=" + shutdownEmpty.Text);
FullConfig = FullConfig.Replace("ResumeProspect=" + LoadSetting(ConfigFile, "ResumeProspect"), "ResumeProspect=" + GetResumeStatus());
File.WriteAllText(ConfigFile, FullConfig);
string FinalConfig = File.ReadAllText(ConfigFile);
if (displayChanges.IsChecked == true)
{
MessageBox.Show(FinalConfig);
}
PS. The Reason to reread it at the end there is that it should have all new values, so if they choose to display changes it helps to verify the file was actually changed
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server