diff --git a/Application/App.xaml.cs b/Application/App.xaml.cs index bf8d0ba..2dcd0af 100644 --- a/Application/App.xaml.cs +++ b/Application/App.xaml.cs @@ -4,30 +4,19 @@ using Common.IO; using Common.Settings; using Common.Wpf.Extensions; using FeedCenter.Properties; -using Microsoft.Win32; using System; using System.Diagnostics; using System.Globalization; -using System.IO; -using System.Reflection; namespace FeedCenter { public partial class App { - #region Debug properties - - private static bool _useDebugPath; - - #endregion - #region Main function [STAThread] public static void Main() { - _useDebugPath = Environment.CommandLine.IndexOf("/debugPath", StringComparison.InvariantCultureIgnoreCase) != -1; - // Create and initialize the app object var app = new App(); app.InitializeComponent(); @@ -46,10 +35,7 @@ namespace FeedCenter using (isolationHandle) { // Set the data directory based on debug or not - AppDomain.CurrentDomain.SetData("DataDirectory", - _useDebugPath - ? Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) - : UserSettingsPath); + AppDomain.CurrentDomain.SetData("DataDirectory", SystemConfiguration.DataDirectory); // Get the generic provider var genericProvider = (GenericSettingsProvider) Settings.Default.Providers[typeof(GenericSettingsProvider).Name]; @@ -67,7 +53,7 @@ namespace FeedCenter genericProvider.DeleteOldVersionsOnUpgrade = false; // Initialize the tracer with the current process ID - Tracer.Initialize(UserSettingsPath, FeedCenter.Properties.Resources.ApplicationName, Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture), false); + Tracer.Initialize(SystemConfiguration.UserSettingsPath, FeedCenter.Properties.Resources.ApplicationName, Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture), false); Current.DispatcherUnhandledException += HandleCurrentDispatcherUnhandledException; AppDomain.CurrentDomain.UnhandledException += HandleCurrentDomainUnhandledException; @@ -87,9 +73,11 @@ namespace FeedCenter var splashWindow = new SplashWindow(); splashWindow.ShowDialog(); - // Update the registry settings - SetStartWithWindows(Settings.Default.StartWithWindows); - SetDefaultFeedReader(Settings.Default.RegisterAsDefaultFeedReader); + // Set whether we should auto-start + Current.SetStartWithWindows(Settings.Default.StartWithWindows); + + // Set whether we should be the default feed reader + SystemConfiguration.SetDefaultFeedReader(Settings.Default.RegisterAsDefaultFeedReader); // Initialize the window mainWindow.Initialize(); @@ -115,94 +103,5 @@ namespace FeedCenter } #endregion - - #region Helpers - - public static string UserSettingsPath - { - get - { - // If we're running in debug mode then use a local path for the database and logs - if (_useDebugPath) - return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); - - // Get the path to the local application data directory - var path = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - FeedCenter.Properties.Resources.ApplicationName); - - // Make sure it exists - create it if needed - if (!Directory.Exists(path)) - Directory.CreateDirectory(path); - - return path; - } - } - - public static void SetStartWithWindows(bool value) - { - // Get the application name - var applicationName = FeedCenter.Properties.Resources.ApplicationDisplayName; - - // Get application details - var publisherName = applicationName; - var productName = applicationName; - var allProgramsPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs); - var shortcutPath = Path.Combine(allProgramsPath, publisherName); - - // Build the auto start path - shortcutPath = "\"" + Path.Combine(shortcutPath, productName) + ".appref-ms\""; - - // Set auto start - Current.SetStartWithWindows(applicationName, shortcutPath, value); - } - - public static void SetDefaultFeedReader(bool value) - { - // Get the location of the assembly - var assemblyLocation = Assembly.GetEntryAssembly().Location; - - // Open the registry key (creating if needed) - using (var registryKey = Registry.CurrentUser.CreateSubKey("Software\\Classes\\feed", RegistryKeyPermissionCheck.ReadWriteSubTree)) - { - if (registryKey == null) - return; - - // Write the handler settings - registryKey.SetValue(string.Empty, "URL:Feed Handler"); - registryKey.SetValue("URL Protocol", string.Empty); - - // Open the icon subkey (creating if needed) - using (var subKey = registryKey.CreateSubKey("DefaultIcon", RegistryKeyPermissionCheck.ReadWriteSubTree)) - { - if (subKey != null) - { - // Write the assembly location - subKey.SetValue(string.Empty, assemblyLocation); - - // Close the subkey - subKey.Close(); - } - } - - // Open the subkey for the command (creating if needed) - using (var subKey = registryKey.CreateSubKey("shell\\open\\command", RegistryKeyPermissionCheck.ReadWriteSubTree)) - { - if (subKey != null) - { - // Write the assembly location and parameter - subKey.SetValue(string.Empty, string.Format("\"{0}\" %1", assemblyLocation)); - - // Close the subkey - subKey.Close(); - } - } - - // Close the registry key - registryKey.Close(); - } - } - - #endregion } } diff --git a/Application/FeedCenter.csproj b/Application/FeedCenter.csproj index 50b862f..789e79a 100644 --- a/Application/FeedCenter.csproj +++ b/Application/FeedCenter.csproj @@ -183,6 +183,7 @@ + MSBuild:Compile Designer diff --git a/Application/Options/GeneralOptionsPanel.xaml.cs b/Application/Options/GeneralOptionsPanel.xaml.cs index dc49809..2f60a79 100644 --- a/Application/Options/GeneralOptionsPanel.xaml.cs +++ b/Application/Options/GeneralOptionsPanel.xaml.cs @@ -1,4 +1,7 @@ -namespace FeedCenter.Options +using System.Windows; +using Common.Wpf.Extensions; + +namespace FeedCenter.Options { public partial class GeneralOptionsPanel { @@ -32,8 +35,8 @@ if (registerAsDefaultFeedReaderCheckBox.IsChecked.HasValue && settings.RegisterAsDefaultFeedReader != registerAsDefaultFeedReaderCheckBox.IsChecked.Value) settings.RegisterAsDefaultFeedReader = registerAsDefaultFeedReaderCheckBox.IsChecked.Value; - App.SetStartWithWindows(settings.StartWithWindows); - App.SetDefaultFeedReader(settings.RegisterAsDefaultFeedReader); + Application.Current.SetStartWithWindows(settings.StartWithWindows); + SystemConfiguration.SetDefaultFeedReader(settings.RegisterAsDefaultFeedReader); } public override string CategoryName diff --git a/Application/SystemConfiguration.cs b/Application/SystemConfiguration.cs new file mode 100644 index 0000000..8ca958e --- /dev/null +++ b/Application/SystemConfiguration.cs @@ -0,0 +1,95 @@ +using Common.Wpf.Extensions; +using Microsoft.Win32; +using System; +using System.IO; +using System.Reflection; +using System.Windows; + +namespace FeedCenter +{ + public static class SystemConfiguration + { + public static void SetDefaultFeedReader(bool value) + { + // Get the location of the assembly + var assemblyLocation = Assembly.GetEntryAssembly().Location; + + // Open the registry key (creating if needed) + using (var registryKey = Registry.CurrentUser.CreateSubKey("Software\\Classes\\feed", RegistryKeyPermissionCheck.ReadWriteSubTree)) + { + if (registryKey == null) + return; + + // Write the handler settings + registryKey.SetValue(string.Empty, "URL:Feed Handler"); + registryKey.SetValue("URL Protocol", string.Empty); + + // Open the icon subkey (creating if needed) + using (var subKey = registryKey.CreateSubKey("DefaultIcon", RegistryKeyPermissionCheck.ReadWriteSubTree)) + { + if (subKey != null) + { + // Write the assembly location + subKey.SetValue(string.Empty, assemblyLocation); + + // Close the subkey + subKey.Close(); + } + } + + // Open the subkey for the command (creating if needed) + using (var subKey = registryKey.CreateSubKey("shell\\open\\command", RegistryKeyPermissionCheck.ReadWriteSubTree)) + { + if (subKey != null) + { + // Write the assembly location and parameter + subKey.SetValue(string.Empty, string.Format("\"{0}\" %1", assemblyLocation)); + + // Close the subkey + subKey.Close(); + } + } + + // Close the registry key + registryKey.Close(); + } + } + + public static bool UseDebugPath + { + get + { + return Environment.CommandLine.IndexOf("/debugPath", StringComparison.InvariantCultureIgnoreCase) != -1; + } + } + + public static string DataDirectory + { + get + { + return UseDebugPath ? Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) : UserSettingsPath; + } + } + + public static string UserSettingsPath + { + get + { + // If we're running in debug mode then use a local path for the database and logs + if (UseDebugPath) + return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); + + // Get the path to the local application data directory + var path = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + Properties.Resources.ApplicationName); + + // Make sure it exists - create it if needed + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + + return path; + } + } + } +}