コード例 #1
0
        public static string GetSaveFile(
            FileFilterCollectionLegacy filters, bool ensureExtension = true,
            string defaultFileName = "",
            Action <CommonSaveFileDialog> otherSetting = null,
            Window owner = null)
        {
            var dialog = new CommonSaveFileDialog
            {
                AlwaysAppendDefaultExtension = true,
                DefaultFileName = defaultFileName
            };

            filters.Filters.ForEach(p => dialog.Filters.Add(p));
            otherSetting?.Invoke(dialog);
            //dialog.AlwaysAppendDefaultExtension = ensureExtension;

            if (dialog.ShowDialog(owner ?? DefaultOwner.Owner) != CommonFileDialogResult.Ok)
            {
                return(null);
            }
            if (ensureExtension && filters.Filters.Any())
            {
                var filter = filters.Filters.ToArray()[dialog.SelectedFileTypeIndex - 1];
                if (string.IsNullOrEmpty(Path.GetExtension(dialog.FileName)))
                {
                    string newName = dialog.FileName + "." + filter.Extensions.First();
                    filters.Raise(dialog, newName);
                    return(newName);
                }
            }
            filters.Raise(dialog);
            return(dialog.FileName);
        }
コード例 #2
0
        public static string GetOpenFile(
            FileFilterCollectionLegacy filters         = null, string defaultFileName = "",
            Action <CommonOpenFileDialog> otherSetting = null,
            Window owner = null)
        {
            var dialog = GetOpenDialog(defaultFileName, filters);

            otherSetting?.Invoke(dialog);
            if (dialog.ShowDialog(owner ?? DefaultOwner.Owner) != CommonFileDialogResult.Ok)
            {
                return(null);
            }
            filters.Raise(dialog);
            return(dialog.FileName);
        }