Reorganize auto-start and default feed reader settings

This commit is contained in:
2014-12-06 09:07:18 -05:00
parent cd0113ff9c
commit 8f3a532abc
4 changed files with 109 additions and 111 deletions

View File

@@ -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
}
}