diff --git a/VAR.PdfTools.Workbench/Configuration.cs b/VAR.PdfTools.Workbench/Configuration.cs new file mode 100644 index 0000000..b9c9df2 --- /dev/null +++ b/VAR.PdfTools.Workbench/Configuration.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace VAR.PdfTools.Workbench +{ + public class Configuration + { + private Dictionary _configItems = new Dictionary(); + + private static string GetConfigFileName() + { + string location = System.Reflection.Assembly.GetEntryAssembly().Location; + string path = Path.GetDirectoryName(location); + string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location); + + string configFile = string.Format("{0}/{1}.cfg", path, filenameWithoutExtension); + return configFile; + } + + private static string[] GetConfigurationLines() + { + string configFile = GetConfigFileName(); + string[] config; + if (File.Exists(configFile) == false) + { + config = new string[0]; + } + else + { + config = File.ReadAllLines(configFile); + } + return config; + } + + public void Load() + { + _configItems.Clear(); + string[] configLines = GetConfigurationLines(); + foreach (string configLine in configLines) + { + int idxSplit = configLine.IndexOf('|'); + if (idxSplit < 0) { continue; } + string configName = configLine.Substring(0, idxSplit); + string configData = configLine.Substring(idxSplit + 1); + + if (_configItems.ContainsKey(configName)) + { + _configItems[configName] = configData; + } + else + { + _configItems.Add(configName, configData); + } + } + } + + public void Save() + { + StringBuilder sbConfig = new StringBuilder(); + foreach (KeyValuePair pair in _configItems) + { + sbConfig.AppendFormat("{0}|{1}\n", pair.Key, pair.Value); + } + string configFileName = GetConfigFileName(); + File.WriteAllText(configFileName, sbConfig.ToString()); + } + + public string Get(string key, string defaultValue) + { + if (_configItems == null) { return defaultValue; } + if (_configItems.ContainsKey(key)) + { + return _configItems[key]; + } + return defaultValue; + } + + public bool Get(string key, bool defaultValue) + { + if (_configItems == null) { return defaultValue; } + if (_configItems.ContainsKey(key)) + { + string value = _configItems[key]; + return (value == "true"); + } + return defaultValue; + } + + public void Set(string key, string value) + { + if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) + { + _configItems[key] = value; + } + else + { + _configItems.Add(key, value); + } + } + + public void Set(string key, bool value) + { + if (_configItems == null) { return; } + if (_configItems.ContainsKey(key)) + { + _configItems[key] = value ? "true" : "false"; + } + else + { + _configItems.Add(key, value ? "true" : "false"); + } + } + + } +} diff --git a/VAR.PdfTools.Workbench/FrmPdfInfo.cs b/VAR.PdfTools.Workbench/FrmPdfInfo.cs index bb34ce9..ac1d431 100644 --- a/VAR.PdfTools.Workbench/FrmPdfInfo.cs +++ b/VAR.PdfTools.Workbench/FrmPdfInfo.cs @@ -19,19 +19,27 @@ namespace VAR.PdfTools.Workbench private void FrmPdfInfo_Load(object sender, EventArgs e) { - txtPdfPath.Text = Properties.Settings.Default.LastPdfPath; - txtField1.Text = Properties.Settings.Default.Field1; - txtField2.Text = Properties.Settings.Default.Field2; - txtField3.Text = Properties.Settings.Default.Field3; + var configuration = new Configuration(); + configuration.Load(); + txtPdfPath.Text = configuration.Get("LastPdfPath", string.Empty); + txtField1.Text = configuration.Get("Field1", string.Empty); + txtField2.Text = configuration.Get("Field2", string.Empty); + txtField3.Text = configuration.Get("Field3", string.Empty); + txtPages.Text = configuration.Get("Pages", string.Empty); + chkRender.Checked = configuration.Get("Render", false); } private void FrmPdfInfo_FormClosing(object sender, FormClosingEventArgs e) { - Properties.Settings.Default.LastPdfPath = txtPdfPath.Text; - Properties.Settings.Default.Field1 = txtField1.Text; - Properties.Settings.Default.Field2 = txtField2.Text; - Properties.Settings.Default.Field3 = txtField3.Text; - Properties.Settings.Default.Save(); + var configuration = new Configuration(); + var configItems = new Dictionary(); + configuration.Set("LastPdfPath", txtPdfPath.Text); + configuration.Set("Field1", txtField1.Text); + configuration.Set("Field2", txtField2.Text); + configuration.Set("Field3", txtField3.Text); + configuration.Set("Pages", txtPages.Text); + configuration.Set("Render", chkRender.Checked); + configuration.Save(); } private void btnBrowse_Click(object sender, EventArgs e) diff --git a/VAR.PdfTools.Workbench/Properties/Settings.Designer.cs b/VAR.PdfTools.Workbench/Properties/Settings.Designer.cs deleted file mode 100644 index 780247a..0000000 --- a/VAR.PdfTools.Workbench/Properties/Settings.Designer.cs +++ /dev/null @@ -1,74 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace VAR.PdfTools.Workbench.Properties { - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default { - get { - return defaultInstance; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] - public string LastPdfPath { - get { - return ((string)(this["LastPdfPath"])); - } - set { - this["LastPdfPath"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] - public string Field1 { - get { - return ((string)(this["Field1"])); - } - set { - this["Field1"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] - public string Field2 { - get { - return ((string)(this["Field2"])); - } - set { - this["Field2"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] - public string Field3 { - get { - return ((string)(this["Field3"])); - } - set { - this["Field3"] = value; - } - } - } -} diff --git a/VAR.PdfTools.Workbench/Properties/Settings.settings b/VAR.PdfTools.Workbench/Properties/Settings.settings deleted file mode 100644 index 523c69e..0000000 --- a/VAR.PdfTools.Workbench/Properties/Settings.settings +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/VAR.PdfTools.Workbench/VAR.PdfTools.Workbench.csproj b/VAR.PdfTools.Workbench/VAR.PdfTools.Workbench.csproj index 25f6ad2..642888a 100644 --- a/VAR.PdfTools.Workbench/VAR.PdfTools.Workbench.csproj +++ b/VAR.PdfTools.Workbench/VAR.PdfTools.Workbench.csproj @@ -48,6 +48,7 @@ + Form @@ -56,16 +57,6 @@ - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - True - Settings.settings - True - diff --git a/VAR.PdfTools.Workbench/app.config b/VAR.PdfTools.Workbench/app.config deleted file mode 100644 index 4cd9d7b..0000000 --- a/VAR.PdfTools.Workbench/app.config +++ /dev/null @@ -1,24 +0,0 @@ - - - - -
- - - - - - - - - - - - - - - - - - - \ No newline at end of file