コード例 #1
0
        /// <inheritdoc/>
        public async Task <IStorageFile> RequestFileOpenAsync(StorageDialogSettings settings)
        {
            IEnumerable <string> fileTypes = settings.ShownFileTypes?.Select(t => $".{t}");

            if (fileTypes == null || !fileTypes.Any())
            {
                Console.Write($"Select file (.*): ");
            }
            else
            {
                Console.Write($"Select file ({string.Join(",", fileTypes)}): ");
            }
            string name = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                return(null);
            }
            else
            {
                var fileInfo = new FileInfo(name);
                if (fileInfo.Exists && (fileTypes == null || !fileTypes.Any() || fileTypes.Contains(fileInfo.Extension)))
                {
                    Console.Write($"{settings.OverrideSelectText ?? "Open"} (y/N)? ");
                    if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                    {
                        return(new BaseFile(fileInfo));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    Console.Write($"File {name} does not exist or is of incorrect type. Try again (y/N)?");
                    if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                    {
                        return(await RequestFileOpenAsync(settings));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
コード例 #2
0
        /// <inheritdoc/>
        public async Task <IStorageFolder> RequestFolderAsync(StorageDialogSettings settings)
        {
            FolderPicker dialog = new FolderPicker();

            if (settings.ShownFileTypes == null || !settings.ShownFileTypes.Any())
            {
                dialog.FileTypeFilter.Add("*");
            }
            else
            {
                dialog.FileTypeFilter.AddRange(settings.ShownFileTypes.Select(f => $".{f}"));
            }

            if (settings.OverrideSelectText != null)
            {
                dialog.CommitButtonText = settings.OverrideSelectText;
            }

            var folder = await dialog.PickSingleFolderAsync();

            return(new UwpFolder(folder));
        }
コード例 #3
0
        /// <inheritdoc/>
        public async Task <IStorageFolder> RequestFolderAsync(StorageDialogSettings settings)
        {
            Console.Write($"Select folder: ");
            string name = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                return(null);
            }
            else
            {
                var dirInfo = new DirectoryInfo(name);
                if (dirInfo.Exists)
                {
                    Console.Write($"{settings.OverrideSelectText ?? "Open"} (y/N)? ");
                    if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                    {
                        return(new BaseFolder(dirInfo));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    Console.Write($"Directory {name} does not exist. Try again (y/N)?");
                    if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                    {
                        return(await RequestFolderAsync(settings));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
コード例 #4
0
        /// <inheritdoc/>
        public async Task <IStorageFile> RequestFileSaveAsync(StorageDialogSettings settings)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();

            if (settings.ShownFileTypes != null)
            {
                fileDialog.DefaultExt = $".{settings.ShownFileTypes.First()}";
                fileDialog.Filter     = string.Join(",", settings.ShownFileTypes.Select(e => $".{e}"));
            }
            if (settings.OverrideSelectText != null)
            {
                fileDialog.Title = settings.OverrideSelectText;
            }

            if (fileDialog.ShowDialog() ?? false)
            {
                return(new BaseFile(new System.IO.FileInfo(fileDialog.FileName)));
            }
            else
            {
                throw new StorageAccessException("File dialog closed or failed to find file.");
            }
        }
コード例 #5
0
        /// <inheritdoc/>
        public async Task <IStorageFile> RequestFileSaveAsync(StorageDialogSettings settings)
        {
            FileSavePicker dialog = new FileSavePicker();

            if (settings.ShownFileTypes == null || !settings.ShownFileTypes.Any())
            {
            }
            else
            {
                foreach (var type in settings.ShownFileTypes.Select(f => $".{f}"))
                {
                    dialog.FileTypeChoices.Add(type, new string[] { type });
                }
            }

            if (settings.OverrideSelectText != null)
            {
                dialog.CommitButtonText = settings.OverrideSelectText;
            }

            var file = await dialog.PickSaveFileAsync();

            return(new UwpFile(file));
        }
コード例 #6
0
 /// <inheritdoc/>
 public async Task <IStorageFolder> RequestFolderAsync(StorageDialogSettings settings)
 {
     throw new PlatformNotSupportedException("WPF does not currently provide an API for picking folders.");
 }
コード例 #7
0
        /// <inheritdoc/>
        public async Task <IStorageFile> RequestFileSaveAsync(StorageDialogSettings settings)
        {
            IEnumerable <string> fileTypes = settings.ShownFileTypes?.Select(t => $".{t}");

            if (fileTypes == null || !fileTypes.Any())
            {
                Console.Write($"Select file (.*): ");
            }
            else
            {
                Console.Write($"Select file ({string.Join(",", fileTypes)}): ");
            }
            string name = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                return(null);
            }
            else
            {
                var fileInfo = new FileInfo(name);
                if (!(fileTypes == null || !fileTypes.Any() || fileTypes.Contains(fileInfo.Extension)))
                {
                    if (!Path.HasExtension(fileInfo.FullName))
                    {
                        fileInfo = new FileInfo($"{name}.{fileTypes.First()}");
                    }
                    else
                    {
                        Console.Write($"File {name} is of the wrong type. Try again (y/N)?");
                        if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                        {
                            return(await RequestFileSaveAsync(settings));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }

                if (fileInfo.Exists)
                {
                    Console.Write($"File {name} already exists. Overwrite (y/N)?");
                    if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                    {
                        return(new BaseFile(fileInfo));
                    }
                    else
                    {
                        Console.Write($"Try again (y/N)?");
                        if (string.Equals(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase))
                        {
                            return(await RequestFileSaveAsync(settings));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                else
                {
                    return(new BaseFile(fileInfo));
                }
            }
        }