public static ServerPropertiesDictonary Load(String file) { ServerPropertiesDictonary dict = new ServerPropertiesDictonary(); try { using (StreamReader sr = new StreamReader(file)) { while (!sr.EndOfStream) { String line = sr.ReadLine(); if (line.Length > 0 && !line.Contains("#")) { Match m = Regex.Match(line, "(?<left>[^=]+)=(?<right>[^=]+)",RegexOptions.IgnoreCase); if (m.Success) { String left = m.Groups["left"].Value; String right = m.Groups["right"].Value; dict.Add(left, right); } } } sr.Close(); } } catch ( Exception ex ) { Log.Append(null, "Load Serverproperties " + ex.Message, Log.ExceptionsLog); } return dict; }
private void CheckServerProperties() { try { bool overwrite = false; ServerPropertiesDictonary dict = ServerPropertiesParser.Load(Path.Combine(Application.StartupPath, "server.properties")); if (dict.ContainsKey("server-ip")) { if (dict["server-ip"] != "127.0.0.1") { DialogResult result = MessageBox.Show("ZMA has detected that the server internaly doesn't run with the local IP: 127.0.0.1" + "\nShould ZMA correct the IP ?\nNote that this will overwrite the server.properties!", "\nCorrect Internal IP and overwrite server.properties ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (result == DialogResult.Yes) { dict["server-ip"] = "127.0.0.1"; overwrite = true; } } if (dict["server-port"] != numericInternalPort.Value.ToString()) { DialogResult result = MessageBox.Show("ZMA has detected that the server internaly doesn't run with the internal port: " + numericInternalPort.Value.ToString() + "\nShould ZMA correct the internal Port ?\nNote that this will overwrite the server.properties!", "\nCorrect Internal Port and overwrite server.properties ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (result == DialogResult.Yes) { dict["server-port"] = numericInternalPort.Value.ToString(); overwrite = true; } } if (overwrite) { dict.Save(Path.Combine(Application.StartupPath, "server.properties")); } } } catch (Exception ex) { Log.Append(this, "Write server.properties " + ex.Message, Log.ExceptionsLog); } }