Skip to content

Commit

Permalink
Added check if config exists, if not create
Browse files Browse the repository at this point in the history
  • Loading branch information
voltura committed Oct 26, 2018
1 parent a716af4 commit 35ff50a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
34 changes: 32 additions & 2 deletions WeekNumber/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@ internal static bool StartWithWindows

#region Internal static methods

internal static bool SettingIsValue(string setting, string value) =>
ConfigurationManager.AppSettings.Get(setting) == value;
internal static bool SettingIsValue(string setting, string value)
{
CreateSettings();
return ConfigurationManager.AppSettings.Get(setting) == value;
}

internal static string GetSetting(string setting)
{
CreateSettings();
return ConfigurationManager.AppSettings.Get(setting);
}

internal static void UpdateSetting(string setting, string value)
{
CreateSettings();
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
settings[setting].Value = value;
Expand All @@ -50,5 +60,25 @@ internal static void UpdateSetting(string setting, string value)
}

#endregion Internal static methods

#region Private method that creates the application settings file if needed

private static void CreateSettings()
{
var settingsFile = Application.ExecutablePath + ".config";
if (!System.IO.File.Exists(settingsFile))
{
const string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
<appSettings>
<add key=""DayOfWeek"" value=""Monday""/>
<add key=""CalendarWeekRule"" value=""FirstFourDayWeek""/>
</appSettings>
</configuration>";
System.IO.File.WriteAllText(settingsFile, xml, System.Text.Encoding.UTF8);
}
}

#endregion Private method that creates the application settings file if needed
}
}
2 changes: 1 addition & 1 deletion WeekNumber/TaskbarGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static void CalendarWeekRuleClick(object o, EventArgs e)
m.Checked = false;
}
mi.Checked = newCheckState;
Settings.UpdateSetting(nameof(CalendarWeekRule), mi.Text);
Settings.UpdateSetting(nameof(CalendarWeekRule), mi.Text.Replace(" ", string.Empty));
if (mi != null)
{
mi.Enabled = true;
Expand Down
6 changes: 2 additions & 4 deletions WeekNumber/Week.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#region Using statements

using System;
using System.Configuration;
using System.ComponentModel;
using System.Globalization;

Expand Down Expand Up @@ -53,13 +52,12 @@ public int Current
{
get
{
var dayOfWeekSetting = ConfigurationManager.AppSettings.Get(nameof(DayOfWeek));
var dayOfWeekSetting = Settings.GetSetting(nameof(DayOfWeek));
Enum dayOfWeekEnum = DayOfWeek.Sunday;
var dayOfWeek = (DayOfWeek)
TypeDescriptor.GetConverter(dayOfWeekEnum)
.ConvertFrom(dayOfWeekSetting);
var calendarWeekRuleSetting = ConfigurationManager.AppSettings
.Get(nameof(CalendarWeekRule));
var calendarWeekRuleSetting = Settings.GetSetting(nameof(CalendarWeekRule));
Enum calendarWeekRuleEnum = CalendarWeekRule.FirstFourDayWeek;
var calendarWeekRule = (CalendarWeekRule)
TypeDescriptor.GetConverter(calendarWeekRuleEnum)
Expand Down

0 comments on commit 35ff50a

Please sign in to comment.