Quasar/Server/Settings/Settings.cs

99 lines
3.5 KiB
C#
Raw Normal View History

2014-07-08 12:58:53 +00:00
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
2015-01-13 18:29:11 +00:00
namespace xServer.Settings
2014-07-08 12:58:53 +00:00
{
public static class XMLSettings
{
public const string VERSION = "RELEASE3";
2014-07-08 12:58:53 +00:00
public static ushort ListenPort { get; set; }
2015-01-13 09:12:56 +00:00
public static bool ShowToU { get; set; }
2014-07-08 12:58:53 +00:00
public static bool AutoListen { get; set; }
public static bool ShowPopup { get; set; }
2014-07-30 12:03:00 +00:00
public static bool UseUPnP { get; set; }
public static bool ShowToolTip { get; set; }
2014-07-08 12:58:53 +00:00
public static string Password { get; set; }
private static string _settingsFilePath = Path.Combine(Application.StartupPath, "settings.xml");
2014-07-08 12:58:53 +00:00
public static bool WriteDefaultSettings()
{
try
{
if (!File.Exists(_settingsFilePath))
2014-07-08 12:58:53 +00:00
{
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("settings");
doc.AppendChild(root);
root.AppendChild(doc.CreateElement("ListenPort")).InnerText = "4782";
root.AppendChild(doc.CreateElement("AutoListen")).InnerText = "False";
root.AppendChild(doc.CreateElement("ShowPopup")).InnerText = "False";
root.AppendChild(doc.CreateElement("Password")).InnerText = "1234";
root.AppendChild(doc.CreateElement("ShowToU")).InnerText = "True";
2014-07-30 12:03:00 +00:00
root.AppendChild(doc.CreateElement("UseUPnP")).InnerText = "False";
root.AppendChild(doc.CreateElement("ShowToolTip")).InnerText = "False";
2014-07-08 12:58:53 +00:00
doc.Save(_settingsFilePath);
2014-07-08 12:58:53 +00:00
}
return true;
}
catch
{
return false;
}
}
public static string ReadValue(string pstrValueToRead)
{
try
{
XPathDocument doc = new XPathDocument(_settingsFilePath);
2014-07-08 12:58:53 +00:00
XPathNavigator nav = doc.CreateNavigator();
var expr = nav.Compile(@"/settings/" + pstrValueToRead);
2014-07-08 12:58:53 +00:00
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext())
{
return iterator.Current.Value;
}
2014-07-08 12:58:53 +00:00
return string.Empty;
}
catch
{
return string.Empty;
}
}
public static bool WriteValue(string pstrValueToRead, string pstrValueToWrite)
{
try
{
XmlDocument doc = new XmlDocument();
using (var reader = new XmlTextReader(_settingsFilePath))
{
doc.Load(reader);
}
2014-07-08 12:58:53 +00:00
XmlElement root = doc.DocumentElement;
var oldNode = root.SelectSingleNode(@"/settings/" + pstrValueToRead);
if (oldNode == null) // create if not exist
{
oldNode = doc.SelectSingleNode("settings");
oldNode.AppendChild(doc.CreateElement(pstrValueToRead)).InnerText = pstrValueToWrite;
doc.Save(_settingsFilePath);
return true;
}
2014-07-08 12:58:53 +00:00
oldNode.InnerText = pstrValueToWrite;
doc.Save(_settingsFilePath);
2014-07-08 12:58:53 +00:00
return true;
}
catch
{
return false;
}
}
}
}