-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileFolderDialog.cs
131 lines (120 loc) · 2.63 KB
/
FileFolderDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
namespace Merthsoft.TokenIDE
{
public class FileFolderDialog : CommonDialog
{
private OpenFileDialog dialog = new OpenFileDialog();
public OpenFileDialog Dialog
{
get { return dialog; }
set { dialog = value; }
}
public new DialogResult ShowDialog()
{
return this.ShowDialog(null);
}
public new DialogResult ShowDialog(IWin32Window owner)
{
// Set validate names to false otherwise windows will not let you select "Folder Selection."
dialog.ValidateNames = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = true;
try
{
// Set initial directory (used when dialog.FileName is set from outside)
if (dialog.FileName != null && dialog.FileName != "")
{
if (Directory.Exists(dialog.FileName))
dialog.InitialDirectory = dialog.FileName;
else
dialog.InitialDirectory = Path.GetDirectoryName(dialog.FileName);
}
}
catch {
// Do nothing
}
// Always default to Folder Selection.
dialog.FileName = "Folder Selection.";
if (owner == null)
return dialog.ShowDialog();
else
return dialog.ShowDialog(owner);
}
/// <summary>
// Helper property. Parses FilePath into either folder path (if Folder Selection. is set)
// or returns file path
/// </summary>
public string SelectedPath
{
get {
try
{
if (dialog.FileName != null &&
(dialog.FileName.EndsWith("Folder Selection.") || !File.Exists(dialog.FileName)) &&
!Directory.Exists(dialog.FileName))
{
return Path.GetDirectoryName(dialog.FileName);
}
else
{
return dialog.FileName;
}
}
catch
{
return dialog.FileName;
}
}
set
{
if (value != null && value != "")
{
dialog.FileName = value;
}
}
}
/// <summary>
/// When multiple files are selected returns them as semi-colon seprated string
/// </summary>
public string SelectedPaths
{
get {
if (dialog.FileNames != null && dialog.FileNames.Length > 1)
{
StringBuilder sb = new StringBuilder();
foreach (string fileName in dialog.FileNames)
{
try
{
if (File.Exists(fileName))
sb.Append(fileName + ";");
}
catch
{
// Go to next
}
}
return sb.ToString();
}
else
{
return null;
}
}
}
public override void Reset()
{
dialog.Reset();
}
protected override bool RunDialog(IntPtr hwndOwner)
{
return true;
}
}
}