| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.IO;
|
|---|
| 4 | using System.Linq;
|
|---|
| 5 | using System.Windows.Forms;
|
|---|
| 6 |
|
|---|
| 7 | namespace Common
|
|---|
| 8 | {
|
|---|
| 9 | public class Dialogs
|
|---|
| 10 | {
|
|---|
| 11 | public const string AnyFile = "Any file";
|
|---|
| 12 | public const string AnyFileExt = "*";
|
|---|
| 13 |
|
|---|
| 14 | public static bool OpenFileDialog(string fileName, string[] fileTypes, string defaultExt, out string newFileName)
|
|---|
| 15 | {
|
|---|
| 16 | string filter = string.Join(@"|", fileTypes) + @"|" + AnyFile + @"|*." + AnyFileExt;
|
|---|
| 17 | OpenFileDialog openFileDialog = new OpenFileDialog { Filter = filter, DefaultExt = defaultExt };
|
|---|
| 18 | if (!string.IsNullOrEmpty(fileName))
|
|---|
| 19 | {
|
|---|
| 20 | openFileDialog.FileName = Path.GetFileName(fileName);
|
|---|
| 21 | openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|---|
| 25 | {
|
|---|
| 26 | newFileName = openFileDialog.FileName;
|
|---|
| 27 | return true;
|
|---|
| 28 | }
|
|---|
| 29 | else
|
|---|
| 30 | {
|
|---|
| 31 | newFileName = null;
|
|---|
| 32 | return false;
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public static bool OpenFileDialogMulti(string fileName, string fileType, string fileExtension, out string[] newFileNames)
|
|---|
| 37 | {
|
|---|
| 38 | if (fileExtension.StartsWith(".", StringComparison.Ordinal)) fileExtension = fileExtension.Substring(1);
|
|---|
| 39 | OpenFileDialog openFileDialog = new OpenFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*." + AnyFileExt, DefaultExt = fileExtension };
|
|---|
| 40 | if (!string.IsNullOrEmpty(fileName))
|
|---|
| 41 | {
|
|---|
| 42 | openFileDialog.FileName = Path.GetFileName(fileName);
|
|---|
| 43 | openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | openFileDialog.Multiselect = true;
|
|---|
| 47 |
|
|---|
| 48 | if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|---|
| 49 | {
|
|---|
| 50 | newFileNames = openFileDialog.FileNames;
|
|---|
| 51 | return true;
|
|---|
| 52 | }
|
|---|
| 53 | else
|
|---|
| 54 | {
|
|---|
| 55 | newFileNames = null;
|
|---|
| 56 | return false;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public static bool SaveFileDialog(string fileName, string fileType, string fileExtension, out string newFileName)
|
|---|
| 61 | {
|
|---|
| 62 | if (fileExtension.StartsWith(".", StringComparison.Ordinal)) fileExtension = fileExtension.Substring(1);
|
|---|
| 63 | SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*." + AnyFileExt, DefaultExt = fileExtension };
|
|---|
| 64 | if (!string.IsNullOrEmpty(fileName))
|
|---|
| 65 | {
|
|---|
| 66 | saveFileDialog.FileName = Path.GetFileName(fileName);
|
|---|
| 67 | saveFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|---|
| 71 | {
|
|---|
| 72 | newFileName = saveFileDialog.FileName;
|
|---|
| 73 | return true;
|
|---|
| 74 | }
|
|---|
| 75 | else
|
|---|
| 76 | {
|
|---|
| 77 | newFileName = null;
|
|---|
| 78 | return false;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|