Changeset 15 for Common/Dialogs.cs
- Timestamp:
- Jun 18, 2024, 12:15:33 PM (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/Dialogs.cs
r14 r15 1 using System.IO; 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 2 5 using System.Windows.Forms; 3 6 4 7 namespace Common 5 8 { 6 class Dialogs9 public class Dialogs 7 10 { 8 11 public const string AnyFile = "Any file"; 12 public const string AnyFileExt = "*"; 9 13 10 public static string OpenFileDialog(string fileName, string fileType, string fileExtension)14 public static bool OpenFileDialog(string fileName, string[] fileTypes, string defaultExt, out string newFileName) 11 15 { 12 OpenFileDialog dlg = new OpenFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*.*", DefaultExt = fileExtension }; 16 string filter = string.Join(@"|", fileTypes) + @"|" + AnyFile + @"|*." + AnyFileExt; 17 OpenFileDialog openFileDialog = new OpenFileDialog { Filter = filter, DefaultExt = defaultExt }; 13 18 if (!string.IsNullOrEmpty(fileName)) 14 19 { 15 dlg.FileName = Path.GetFileName(fileName);16 dlg.InitialDirectory = Path.GetDirectoryName(fileName);20 openFileDialog.FileName = Path.GetFileName(fileName); 21 openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); 17 22 } 18 23 19 if ( dlg.ShowDialog() == DialogResult.OK)24 if (openFileDialog.ShowDialog() == DialogResult.OK) 20 25 { 21 fileName = dlg.FileName; 26 newFileName = openFileDialog.FileName; 27 return true; 22 28 } 23 return fileName; 29 else 30 { 31 newFileName = null; 32 return false; 33 } 24 34 } 25 35 26 public static string SaveFileDialog(string fileName, string fileType, string fileExtension)36 public static bool OpenFileDialogMulti(string fileName, string fileType, string fileExtension, out string[] newFileNames) 27 37 { 28 SaveFileDialog dlg = new SaveFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*.*", DefaultExt = fileExtension }; 38 if (fileExtension.StartsWith(".", StringComparison.Ordinal)) fileExtension = fileExtension.Substring(1); 39 OpenFileDialog openFileDialog = new OpenFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*." + AnyFileExt, DefaultExt = fileExtension }; 29 40 if (!string.IsNullOrEmpty(fileName)) 30 41 { 31 dlg.FileName = Path.GetFileName(fileName);32 dlg.InitialDirectory = Path.GetDirectoryName(fileName);42 openFileDialog.FileName = Path.GetFileName(fileName); 43 openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); 33 44 } 34 45 35 if (dlg.ShowDialog() == DialogResult.OK) 46 openFileDialog.Multiselect = true; 47 48 if (openFileDialog.ShowDialog() == DialogResult.OK) 36 49 { 37 fileName = dlg.FileName; 50 newFileNames = openFileDialog.FileNames; 51 return true; 38 52 } 39 return fileName; 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 } 40 80 } 41 81 }
Note:
See TracChangeset
for help on using the changeset viewer.