示例#1
0
        public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] byte[] data,
                                          [CanBeNull] ref string filename)
        {
            if (data == null)
            {
                return(false);
            }

            var presetsDirectory = EnsureDirectory(category);

            if (filename != null && !FileUtils.IsAffectedBy(filename, presetsDirectory))
            {
                filename = Path.Combine(presetsDirectory, Path.GetFileName(filename));
            }

            filename = FileRelatedDialogs.Save(new SaveDialogParams {
                Filters          = { new DialogFilterPiece("Presets", "*" + category.Extension) },
                InitialDirectory = presetsDirectory,
                DetaultExtension = category.Extension,
                CustomPlaces     =
                {
                    new FileDialogCustomPlace(presetsDirectory)
                }
            }, filename);
            if (filename == null)
            {
                return(false);
            }

            Logging.Debug("presetsDirectory=" + presetsDirectory);
            Logging.Debug("filename=" + filename);

            if (!FileUtils.IsAffectedBy(filename, presetsDirectory))
            {
                if (ModernDialog.ShowMessage(ToolsStrings.Presets_ChooseFileInInitialDirectory,
                                             ToolsStrings.Common_CannotDo_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    return(SavePresetUsingDialog(key, category, data, ref filename));
                }

                return(false);
            }

            File.WriteAllBytes(filename, data);
            Logging.Debug("Preset saved as " + filename);

            if (key != null)
            {
                PresetSaved?.Invoke(this, new PresetSavedEventArgs(key, filename));
            }

            return(true);
        }
示例#2
0
        public bool SavePresetUsingDialog([CanBeNull] string key, [NotNull] PresetsCategory category, [CanBeNull] string data,
                                          [CanBeNull] ref string filename)
        {
            if (data == null)
            {
                return(false);
            }

            var presetsDirectory = EnsureDirectory(category);

            var dialog = new SaveFileDialog {
                InitialDirectory = presetsDirectory,
                Filter           = string.Format(ToolsStrings.Presets_FileFilter, category.Extension),
                DefaultExt       = category.Extension,
                CustomPlaces     =
                {
                    new FileDialogCustomPlace(presetsDirectory)
                }
            };

            if (filename != null)
            {
                dialog.InitialDirectory = Path.GetDirectoryName(filename);
                dialog.FileName         = Path.GetFileNameWithoutExtension(filename);
            }

            if (dialog.ShowDialog() != true)
            {
                return(false);
            }

            filename = dialog.FileName;
            if (!filename.StartsWith(presetsDirectory))
            {
                if (ModernDialog.ShowMessage(ToolsStrings.Presets_ChooseFileInInitialDirectory,
                                             ToolsStrings.Common_CannotDo_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    return(SavePresetUsingDialog(key, category, data, ref filename));
                }

                return(false);
            }

            File.WriteAllText(filename, data);
            Logging.Debug("Preset saved as " + filename);

            if (key != null)
            {
                PresetSaved?.Invoke(this, new PresetSavedEventArgs(key, filename));
            }

            return(true);
        }