Quasar/Server/Settings/ProfileManager.cs

117 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.IO;
2014-07-08 12:58:53 +00:00
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 class ProfileManager
{
private readonly string _settingsFilePath;
2014-07-08 12:58:53 +00:00
public ProfileManager(string settingsFile)
{
if (string.IsNullOrEmpty(settingsFile)) throw new ArgumentException();
_settingsFilePath = Path.Combine(Application.StartupPath, "Profiles\\" + settingsFile);
2014-07-08 12:58:53 +00:00
try
{
if (!File.Exists(_settingsFilePath))
2014-07-08 12:58:53 +00:00
{
if (!Directory.Exists(Path.GetDirectoryName(_settingsFilePath)))
Directory.CreateDirectory(Path.GetDirectoryName(_settingsFilePath));
2014-07-08 12:58:53 +00:00
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("settings");
doc.AppendChild(root);
2015-08-03 15:33:50 +00:00
root.AppendChild(doc.CreateElement("Hosts"));
2014-07-08 12:58:53 +00:00
root.AppendChild(doc.CreateElement("Password")).InnerText = "1234";
root.AppendChild(doc.CreateElement("Delay")).InnerText = "5000";
root.AppendChild(doc.CreateElement("Mutex"));
root.AppendChild(doc.CreateElement("InstallClient")).InnerText = "False";
root.AppendChild(doc.CreateElement("InstallName"));
root.AppendChild(doc.CreateElement("InstallPath")).InnerText = "1";
root.AppendChild(doc.CreateElement("InstallSub"));
root.AppendChild(doc.CreateElement("HideFile")).InnerText = "False";
root.AppendChild(doc.CreateElement("AddStartup")).InnerText = "False";
root.AppendChild(doc.CreateElement("RegistryName"));
root.AppendChild(doc.CreateElement("AdminElevation")).InnerText = "False";
root.AppendChild(doc.CreateElement("ChangeIcon")).InnerText = "False";
root.AppendChild(doc.CreateElement("ChangeAsmInfo")).InnerText = "False";
2015-05-26 14:14:03 +00:00
root.AppendChild(doc.CreateElement("Keylogger")).InnerText = "False";
root.AppendChild(doc.CreateElement("ProductName"));
root.AppendChild(doc.CreateElement("Description"));
root.AppendChild(doc.CreateElement("CompanyName"));
root.AppendChild(doc.CreateElement("Copyright"));
root.AppendChild(doc.CreateElement("Trademarks"));
root.AppendChild(doc.CreateElement("OriginalFilename"));
root.AppendChild(doc.CreateElement("ProductVersion"));
root.AppendChild(doc.CreateElement("FileVersion"));
2014-07-08 12:58:53 +00:00
doc.Save(_settingsFilePath);
2014-07-08 12:58:53 +00:00
}
}
catch
{
}
2014-07-08 12:58:53 +00:00
}
public string ReadValue(string pstrValueToRead)
{
try
{
XPathDocument doc = new XPathDocument(_settingsFilePath);
2014-07-08 12:58:53 +00:00
XPathNavigator nav = doc.CreateNavigator();
XPathExpression 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;
}
return string.Empty;
}
catch
{
return string.Empty;
}
}
2015-05-26 14:14:03 +00:00
public string ReadValueSafe(string pstrValueToRead, string defaultValue = "")
{
string value = ReadValue(pstrValueToRead);
return (!string.IsNullOrEmpty(value)) ? value : defaultValue;
}
2014-07-08 12:58:53 +00:00
public 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;
XmlNode 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;
}
}
}
}