More settings and profile fixes

ref #197
This commit is contained in:
MaxXor 2015-05-26 16:26:04 +02:00
parent 7a1e6a82ab
commit 1f55275c2b
3 changed files with 21 additions and 19 deletions

View File

@ -11,6 +11,7 @@ namespace xServer.Forms
{ {
public partial class FrmBuilder : Form public partial class FrmBuilder : Form
{ {
private bool _profileLoaded;
private bool _changed; private bool _changed;
public FrmBuilder() public FrmBuilder()
@ -20,7 +21,7 @@ namespace xServer.Forms
private void HasChanged() private void HasChanged()
{ {
if (!_changed) if (!_changed && _profileLoaded)
_changed = true; _changed = true;
} }
@ -63,6 +64,7 @@ namespace xServer.Forms
txtOriginalFilename.Text = pm.ReadValue("OriginalFilename"); txtOriginalFilename.Text = pm.ReadValue("OriginalFilename");
txtProductVersion.Text = pm.ReadValue("ProductVersion"); txtProductVersion.Text = pm.ReadValue("ProductVersion");
txtFileVersion.Text = pm.ReadValue("FileVersion"); txtFileVersion.Text = pm.ReadValue("FileVersion");
_profileLoaded = true;
} }
private void SaveProfile(string profilename) private void SaveProfile(string profilename)

View File

@ -1,4 +1,5 @@
using System.IO; using System;
using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using System.Xml; using System.Xml;
using System.Xml.XPath; using System.Xml.XPath;
@ -7,18 +8,19 @@ namespace xServer.Settings
{ {
public class ProfileManager public class ProfileManager
{ {
private string settingsFilePath { get; set; } private readonly string _settingsFilePath;
public ProfileManager(string settingsFile) public ProfileManager(string settingsFile)
{ {
settingsFilePath = Path.Combine(Application.StartupPath, "Profiles\\" + settingsFile); if (string.IsNullOrEmpty(settingsFile)) throw new ArgumentException();
_settingsFilePath = Path.Combine(Application.StartupPath, "Profiles\\" + settingsFile);
try try
{ {
if (!File.Exists(settingsFilePath)) if (!File.Exists(_settingsFilePath))
{ {
if (!Directory.Exists(Path.GetDirectoryName(settingsFilePath))) if (!Directory.Exists(Path.GetDirectoryName(_settingsFilePath)))
Directory.CreateDirectory(Path.GetDirectoryName(settingsFilePath)); Directory.CreateDirectory(Path.GetDirectoryName(_settingsFilePath));
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateElement("settings"); XmlNode root = doc.CreateElement("settings");
@ -49,7 +51,7 @@ namespace xServer.Settings
root.AppendChild(doc.CreateElement("ProductVersion")); root.AppendChild(doc.CreateElement("ProductVersion"));
root.AppendChild(doc.CreateElement("FileVersion")); root.AppendChild(doc.CreateElement("FileVersion"));
doc.Save(settingsFilePath); doc.Save(_settingsFilePath);
} }
} }
catch catch
@ -61,10 +63,9 @@ namespace xServer.Settings
{ {
try try
{ {
XPathDocument doc = new XPathDocument(settingsFilePath); XPathDocument doc = new XPathDocument(_settingsFilePath);
XPathNavigator nav = doc.CreateNavigator(); XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr; XPathExpression expr = nav.Compile(@"/settings/" + pstrValueToRead);
expr = nav.Compile(@"/settings/" + pstrValueToRead);
XPathNodeIterator iterator = nav.Select(expr); XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext()) while (iterator.MoveNext())
{ {
@ -89,24 +90,23 @@ namespace xServer.Settings
{ {
try try
{ {
XmlNode oldNode;
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
using (var reader = new XmlTextReader(settingsFilePath)) using (var reader = new XmlTextReader(_settingsFilePath))
{ {
doc.Load(reader); doc.Load(reader);
} }
XmlElement root = doc.DocumentElement; XmlElement root = doc.DocumentElement;
oldNode = root.SelectSingleNode(@"/settings/" + pstrValueToRead); XmlNode oldNode = root.SelectSingleNode(@"/settings/" + pstrValueToRead);
if (oldNode == null) // create if not exist if (oldNode == null) // create if not exist
{ {
oldNode = doc.SelectSingleNode("settings"); oldNode = doc.SelectSingleNode("settings");
oldNode.AppendChild(doc.CreateElement(pstrValueToRead)).InnerText = pstrValueToWrite; oldNode.AppendChild(doc.CreateElement(pstrValueToRead)).InnerText = pstrValueToWrite;
doc.Save(settingsFilePath); doc.Save(_settingsFilePath);
return true; return true;
} }
oldNode.InnerText = pstrValueToWrite; oldNode.InnerText = pstrValueToWrite;
doc.Save(settingsFilePath); doc.Save(_settingsFilePath);
return true; return true;
} }
catch catch

View File

@ -21,7 +21,7 @@ namespace xServer.Settings
public static string NoIPPassword { get; set; } public static string NoIPPassword { get; set; }
private static string _settingsFilePath = Path.Combine(Application.StartupPath, "settings.xml"); private static readonly string _settingsFilePath = Path.Combine(Application.StartupPath, "settings.xml");
public static bool WriteDefaultSettings() public static bool WriteDefaultSettings()
{ {
@ -62,7 +62,7 @@ namespace xServer.Settings
{ {
XPathDocument doc = new XPathDocument(_settingsFilePath); XPathDocument doc = new XPathDocument(_settingsFilePath);
XPathNavigator nav = doc.CreateNavigator(); XPathNavigator nav = doc.CreateNavigator();
var expr = nav.Compile(@"/settings/" + pstrValueToRead); XPathExpression expr = nav.Compile(@"/settings/" + pstrValueToRead);
XPathNodeIterator iterator = nav.Select(expr); XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext()) while (iterator.MoveNext())
{ {
@ -94,7 +94,7 @@ namespace xServer.Settings
} }
XmlElement root = doc.DocumentElement; XmlElement root = doc.DocumentElement;
var oldNode = root.SelectSingleNode(@"/settings/" + pstrValueToRead); XmlNode oldNode = root.SelectSingleNode(@"/settings/" + pstrValueToRead);
if (oldNode == null) // create if not exist if (oldNode == null) // create if not exist
{ {
oldNode = doc.SelectSingleNode("settings"); oldNode = doc.SelectSingleNode("settings");