/// <summary> /// Transfers settings from/to the stick according to the direction. /// </summary> /// <param name="direction">The direction.</param> /// <remarks>Documented by Dev02, 2007-12-06</remarks> static void SettingsOnStick(SettingsOnStickDirection direction) { if (!RunningFromStick()) return; if (direction == SettingsOnStickDirection.Save) { //replace settings with placeholders Properties.Settings.Default.DicDir = ReplacePath(Properties.Settings.Default.DicDir, SettingsOnStickReplaceDirection.PathToPlaceholder); Properties.Settings.Default.RecentFiles = ReplacePath(Properties.Settings.Default.RecentFiles, SettingsOnStickReplaceDirection.PathToPlaceholder); //save current properties before copying to stick Properties.Settings.Default.Save(); } System.Configuration.Configuration localconfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); FileInfo sourcefile, destfile; string userconfigfileformat = Properties.Resources.SETUP_STICKMODE_CONFIGFILE; switch (direction) { case SettingsOnStickDirection.Load: sourcefile = new FileInfo(string.Format(userconfigfileformat, config.FilePath)); destfile = new FileInfo(localconfig.FilePath); break; case SettingsOnStickDirection.Save: sourcefile = new FileInfo(localconfig.FilePath); destfile = new FileInfo(string.Format(userconfigfileformat, config.FilePath)); break; default: return; } try { sourcefile.Directory.Create(); destfile.Directory.Create(); File.Copy(sourcefile.FullName, destfile.FullName, true); } catch { return; } //load current properties after loading from stick try { Properties.Settings.Default.Reload(); Properties.Settings.Default.DicDir = ReplacePath(Properties.Settings.Default.DicDir, SettingsOnStickReplaceDirection.PlaceholderToPath); Properties.Settings.Default.RecentFiles = ReplacePath(Properties.Settings.Default.RecentFiles, SettingsOnStickReplaceDirection.PlaceholderToPath); Properties.Settings.Default.Save(); } catch (System.Configuration.ConfigurationErrorsException exp) { bool errorfixed = false; if (exp.InnerException != null && exp.InnerException is System.Configuration.ConfigurationErrorsException) { System.Configuration.ConfigurationErrorsException innerexception = exp.InnerException as System.Configuration.ConfigurationErrorsException; if (System.IO.File.Exists(innerexception.Filename)) { try { System.IO.File.Delete(innerexception.Filename); errorfixed = true; } catch { } } } if (System.IO.File.Exists(exp.Filename)) { try { System.IO.File.Delete(exp.Filename); } catch { } } if (System.IO.File.Exists(sourcefile.FullName)) { try { System.IO.File.Delete(sourcefile.FullName); errorfixed = true; } catch { } } if (errorfixed) { Program.MainForm.BringToFront(); Program.MainForm.TopMost = true; Application.DoEvents(); MessageBox.Show(Properties.Resources.INITIALIZE_SETTINGS_ERROR, Properties.Resources.INITIALIZE_SETTINGS_CAPTION); Environment.Exit(-1); } else { throw exp; } } catch (Exception exp) { throw exp; } }
/// <summary> /// Transfers settings from/to the stick according to the direction. /// </summary> /// <param name="direction">The direction.</param> /// <remarks>Documented by Dev02, 2007-12-06</remarks> static void SettingsOnStick(SettingsOnStickDirection direction) { if (!RunningFromStick()) { return; } if (direction == SettingsOnStickDirection.Save) { //replace settings with placeholders Properties.Settings.Default.DicDir = ReplacePath(Properties.Settings.Default.DicDir, SettingsOnStickReplaceDirection.PathToPlaceholder); Properties.Settings.Default.RecentFiles = ReplacePath(Properties.Settings.Default.RecentFiles, SettingsOnStickReplaceDirection.PathToPlaceholder); //save current properties before copying to stick Properties.Settings.Default.Save(); } System.Configuration.Configuration localconfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); FileInfo sourcefile, destfile; string userconfigfileformat = Properties.Resources.SETUP_STICKMODE_CONFIGFILE; switch (direction) { case SettingsOnStickDirection.Load: sourcefile = new FileInfo(string.Format(userconfigfileformat, config.FilePath)); destfile = new FileInfo(localconfig.FilePath); break; case SettingsOnStickDirection.Save: sourcefile = new FileInfo(localconfig.FilePath); destfile = new FileInfo(string.Format(userconfigfileformat, config.FilePath)); break; default: return; } try { sourcefile.Directory.Create(); destfile.Directory.Create(); File.Copy(sourcefile.FullName, destfile.FullName, true); } catch { return; } //load current properties after loading from stick try { Properties.Settings.Default.Reload(); Properties.Settings.Default.DicDir = ReplacePath(Properties.Settings.Default.DicDir, SettingsOnStickReplaceDirection.PlaceholderToPath); Properties.Settings.Default.RecentFiles = ReplacePath(Properties.Settings.Default.RecentFiles, SettingsOnStickReplaceDirection.PlaceholderToPath); Properties.Settings.Default.Save(); } catch (System.Configuration.ConfigurationErrorsException exp) { bool errorfixed = false; if (exp.InnerException != null && exp.InnerException is System.Configuration.ConfigurationErrorsException) { System.Configuration.ConfigurationErrorsException innerexception = exp.InnerException as System.Configuration.ConfigurationErrorsException; if (System.IO.File.Exists(innerexception.Filename)) { try { System.IO.File.Delete(innerexception.Filename); errorfixed = true; } catch { } } } if (System.IO.File.Exists(exp.Filename)) { try { System.IO.File.Delete(exp.Filename); } catch { } } if (System.IO.File.Exists(sourcefile.FullName)) { try { System.IO.File.Delete(sourcefile.FullName); errorfixed = true; } catch { } } if (errorfixed) { Program.MainForm.BringToFront(); Program.MainForm.TopMost = true; Application.DoEvents(); MessageBox.Show(Properties.Resources.INITIALIZE_SETTINGS_ERROR, Properties.Resources.INITIALIZE_SETTINGS_CAPTION); Environment.Exit(-1); } else { throw exp; } } catch (Exception exp) { throw exp; } }