Esempio n. 1
0
        private static void Main()
        {
            Application.ThreadException += WinFormExceptionHandler.OnThreadException;
            AppDomain.CurrentDomain.UnhandledException += WinFormExceptionHandler.OnUnhandledCLRException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (Environment.OSVersion.Version.Major < 6)
            {
                MessageBox.Show("Audio Switcher only supports Windows Vista and Windows 7",
                    "Unsupported Operating System");
                return;
            }

            Application.ApplicationExit += Application_ApplicationExit;

            //Delete the old updater
            try
            {
                var updaterPath = Application.StartupPath + "AutoUpdater.exe";
                if (File.Exists(updaterPath))
                    File.Delete(updaterPath);
            }
            catch
            {
                //This shouldn't prevent the application from running
            }

            //Delete the new updater
            try
            {
                var updaterPath = Path.Combine(Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName,
                    "AutoUpdater.exe");
                if (File.Exists(updaterPath))
                    File.Delete(updaterPath);
            }
            catch
            {
                //This shouldn't prevent the application from running
            }

            var settingsPath = "";
            try
            {
                //Load/Create default settings

                var oldSettingsPath = Path.Combine(Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName,
                    Resources.OldConfigFile);

                settingsPath = oldSettingsPath;

                //1. Provide early notification that the user does not have permission to write.
                new FileIOPermission(FileIOPermissionAccess.Write, settingsPath).Demand();

                settingsPath = Path.Combine(Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName,
                    Resources.ConfigFile);
                new FileIOPermission(FileIOPermissionAccess.Write, settingsPath).Demand();

                //Open and close the settings file to ensure write access
                File.Open(settingsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite).Close();

                ISettingsSource jsonSource = new JsonSettings();
                jsonSource.SetFilePath(settingsPath);

                Settings = new ConfigurationSettings(jsonSource);

                if (File.Exists(oldSettingsPath))
                {
                    try
                    {
                        //Load old settings and copy them to json
                        ISettingsSource iniSource = new IniSettings();
                        iniSource.SetFilePath(oldSettingsPath);

                        var oldSettings = new ConfigurationSettings(iniSource);
                        Settings.LoadFrom(oldSettings);
                    }
                    catch
                    {
                        Settings.CreateDefaults();
                    }
                    finally
                    {
                        File.Delete(oldSettingsPath);
                    }
                }

                Settings.CreateDefaults();
            }
            catch
            {
                MessageBox.Show(
                    string.Format(
                        "Error creating/reading settings file [{0}]. Make sure you have read/write access to this file.\r\nOr try running as Administrator",
                        settingsPath),
                    "Setings File - Cannot Access");
                return;
            }

            try
            {
                Application.Run(AudioSwitcher.Instance);
            }
            catch (Exception ex)
            {
                var title = "An Unexpected Error Occurred";
                var text = ex.Message + Environment.NewLine + Environment.NewLine + ex;

                var edf = new ExceptionDisplayForm(title, ex);
                edf.ShowDialog();
            }
        }
Esempio n. 2
0
        private static void Main()
        {
            Application.ThreadException += WinFormExceptionHandler.OnThreadException;
            AppDomain.CurrentDomain.UnhandledException += WinFormExceptionHandler.OnUnhandledCLRException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (Environment.OSVersion.Version.Major < 6)
            {
                MessageBox.Show("Audio Switcher only supports Windows Vista and above", "Unsupported Operating System");
                return;
            }

            Application.ApplicationExit += Application_ApplicationExit;
            AppDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AudioSwitcher");

            if (!Directory.Exists(AppDataDirectory))
                Directory.CreateDirectory(AppDataDirectory);

            var settingsPath = Path.Combine(AppDataDirectory, Resources.ConfigFile);

            //Delete the old updater
            try
            {
                //v1.5 and less
                var updaterPath = Application.StartupPath + "AutoUpdater.exe";
                if (File.Exists(updaterPath))
                    File.Delete(updaterPath);

                //v1.6
                updaterPath = Path.Combine(Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName, "AutoUpdater.exe");
                if (File.Exists(updaterPath))
                    File.Delete(updaterPath);

                //v1.6.7
                updaterPath = Path.Combine(AppDataDirectory, "AutoUpdater.exe");
                if (File.Exists(updaterPath))
                    File.Delete(updaterPath);
            }
            catch
            {
                //This shouldn't prevent the application from running
            }

            try
            {
                var iniSettingsPath = Path.Combine(Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName, Resources.OldConfigFile);

                if (File.Exists(iniSettingsPath))
                    File.Delete(iniSettingsPath);
            }
            catch
            {
                // ignored
            }

            try
            {

                //old json settings
                var oldJsonSettingsPath = Path.Combine(Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName, Resources.ConfigFile);

                ISettingsSource jsonSource = new JsonSettings();
                jsonSource.SetFilePath(settingsPath);

                Settings = new ConfigurationSettings(jsonSource);

                if (File.Exists(oldJsonSettingsPath))
                {
                    try
                    {
                        //Load old settings
                        ISettingsSource oldSource = new JsonSettings();
                        oldSource.SetFilePath(oldJsonSettingsPath);

                        var oldSettings = new ConfigurationSettings(oldSource);
                        Settings.LoadFrom(oldSettings);
                    }
                    finally
                    {
                        File.Delete(oldJsonSettingsPath);
                    }
                }

                Settings.CreateDefaults();
            }
            catch
            {
                var errorMessage = String.Format("Error creating/reading settings file [{0}]. Make sure you have read/write access to this file.\r\nOr try running as Administrator",
                        settingsPath);
                MessageBox.Show(errorMessage, "Settings File - Cannot Access");
                return;
            }

            try
            {
                Application.Run(AudioSwitcher.Instance);
            }
            catch (Exception ex)
            {
                var title = "An Unexpected Error Occurred";

                var edf = new ExceptionDisplayForm(title, ex);
                edf.ShowDialog();
            }
        }