コード例 #1
0
        public bool SaveMainConfiguration(string fileName, bool saveAs)
        {
            try
            {
                if (!sourceModel.IsValidForSave() || !EnvironmentsCanSave())
                {
                    uiService.ShowError(Resources.SaveConfigurationInvalidError);
                    return(false);
                }

                if (String.IsNullOrEmpty(fileName))
                {
                    return(SaveAs());
                }

                if (!EnsureCanSaveConfigurationFile(fileName))
                {
                    return(false);
                }

                if (saveAs && !string.IsNullOrEmpty(this.configurationFileName) && File.Exists(this.configurationFileName))
                {
                    File.Copy(this.configurationFileName, fileName, true);

                    // make the target file writable (it may have inherited the read-only attribute from the original file)
                    FileAttributes attributes = File.GetAttributes(fileName);
                    File.SetAttributes(fileName, attributes & ~FileAttributes.ReadOnly);
                }


                using (var configurationSource = new DesignConfigurationSource(fileName))
                {
                    var saved = sourceModel.Save(configurationSource);
                    if (!saved)
                    {
                        return(false);
                    }
                }
                ConfigurationFilePath = fileName;
                return(true);
            }
            catch (Exception ex)
            {
                ConfigurationLogWriter.LogException(ex);
                uiService.ShowMessageWpf(string.Format(CultureInfo.CurrentCulture, Resources.SaveApplicationErrorMessage, ex.Message),
                                         Resources.SaveApplicationErrorMessageTitle, MessageBoxButton.OK);

                return(false);
            }
        }
コード例 #2
0
        public bool EnsureCanSaveConfigurationFile(string configurationFile)
        {
            if (!File.Exists(configurationFile))
            {
                using (var textWriter = File.CreateText(configurationFile))
                {
                    textWriter.WriteLine("<configuration />");
                    textWriter.Close();
                }
            }
            else
            {
                var fileIsInvalidConfigurationFile = false;
                try
                {
                    using (var source = new FileConfigurationSource(configurationFile))
                    {
                        source.GetSection("applicationSettings");
                    }
                }
                catch (Exception ex)
                {
                    ConfigurationLogWriter.LogException(ex);
                    var warningResults = uiService.ShowMessageWpf(
                        string.Format(CultureInfo.CurrentCulture, Resources.PromptSaveOverFileThatCannotBeReadFromWarningMessage,
                                      configurationFile),
                        Resources.PromptSaveOverFileThatCannotBeReadFromWarningTitle,
                        MessageBoxButton.OKCancel);

                    if (warningResults == MessageBoxResult.Cancel)
                    {
                        return(false);
                    }
                    fileIsInvalidConfigurationFile = true;
                }

                var attributes = File.GetAttributes(configurationFile);
                if ((attributes | FileAttributes.ReadOnly) == attributes)
                {
                    var overwriteReadonlyDialogResult = uiService.ShowMessageWpf(
                        string.Format(CultureInfo.CurrentCulture, Resources.PromptOverwriteReadonlyFileMessage,
                                      configurationFile),
                        Resources.PromptOverwriteReadonlyFiletitle,
                        MessageBoxButton.YesNoCancel);

                    switch (overwriteReadonlyDialogResult)
                    {
                    case MessageBoxResult.No:

                        return(SaveAs());


                    case MessageBoxResult.Yes:

                        File.SetAttributes(configurationFile, attributes ^ FileAttributes.ReadOnly);

                        break;

                    default:

                        return(false);
                    }
                }

                if (fileIsInvalidConfigurationFile)
                {
                    File.Delete(configurationFile);
                    using (var textWriter = File.CreateText(configurationFile))
                    {
                        textWriter.WriteLine("<configuration />");
                    }
                }
            }
            return(true);
        }