예제 #1
0
 public MainWindow()
 {
     InitializeComponent();
     _configFilePath  = AppDomain.CurrentDomain.BaseDirectory + "Nginx Starter GUI.config.xml";
     _settings        = readConfigFile();
     this.DataContext = _settings;
 }
예제 #2
0
 public MainWindow()
 {
     InitializeComponent();
     _configFilePath = AppDomain.CurrentDomain.BaseDirectory + "Nginx Starter GUI.config.xml";
     _settings = readConfigFile();
     this.DataContext = _settings;
 }
예제 #3
0
        /// <summary>
        /// 从指定文件路径读取程序设置
        /// </summary>
        /// <param name="configFilePath"></param>
        /// <returns>返回程序设置类</returns>
        private Settings.Settings readConfigFile(string configFilePath)
        {
            FileStream fs = null;

            try
            {
                using (fs = File.Open(configFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XmlSerializer     formatter = new XmlSerializer(typeof(Settings.Settings));
                    Settings.Settings settings  = (Settings.Settings)formatter.Deserialize(fs);
                    fs.Close();
                    if (settings == null)
                    {
                        throw new InvalidOperationException("设置文件里什么内容也没有保存");
                    }
                    return(settings);
                }
            }
            catch (FileNotFoundException)
            {
                return(new Settings.Settings());
            }
            catch (InvalidOperationException e)
            {
                MessageBoxResult mb = MessageBox.Show(
                    "设置文件格式错误,是否重建?\n选“是”将备份原有文件后新建设置文件,如果已有备份文件,将会将其替换,选“否”将继续运行,但是将不会保存设置。\n错误详情:" + e.Message,
                    "设置文件格式错误", MessageBoxButton.YesNo, MessageBoxImage.Warning
                    );
                if (mb == MessageBoxResult.Yes)
                {
                    backupConfigFile(configFilePath);
                    return(new Settings.Settings());
                }
                else
                {
                    _inGreenMode = true;
                    return(new Settings.Settings());
                }
            }
            catch (FileLoadException)
            {
                MessageBox.Show("设置文件无法读取,请检查您的权限!程序将继续运行,但是不会保存设置!", "读取设置文件出错", MessageBoxButton.OK, MessageBoxImage.Error);
                _inGreenMode = true;
                return(new Settings.Settings());
            }
            catch
            {
                return(new Settings.Settings());
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }
        }
예제 #4
0
 /// <summary>
 /// 将指定程序设置,保存到指定设置文件
 /// </summary>
 /// <param name="settings">程序设置</param>
 /// <param name="configFilePath">设置文件路径</param>
 private void saveConfigFile(Settings.Settings settings, string configFilePath)
 {
     if (_inGreenMode == true)
     {
         return;
     }
     try
     {
         // 尝试在保存配置文件之前删除旧的文件,希望可以解决之前的错误
         if (File.Exists(configFilePath))
         {
             File.Delete(configFilePath);
         }
         using (FileStream fs = File.Open(configFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
         {
             XmlSerializer formatter = new XmlSerializer(typeof(Settings.Settings));
             formatter.Serialize(fs, settings);
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("保存设置文件失败,您本次的设置可能不会被保存,请确认是否拥有在程序运行目录下的读、写、新建文件权限,或与系统管理员联系!\n错误详情:" + e.Message, "保存设置文件出错", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
예제 #5
0
 /// <summary>
 /// 将指定程序设置,保存到MainWindow类中的默认设置文件路径
 /// </summary>
 /// <param name="settings">程序设置</param>
 private void saveConfigFile(Settings.Settings settings)
 {
     this.saveConfigFile(settings, _configFilePath);
 }