-
Notifications
You must be signed in to change notification settings - Fork 0
/
Folder.vb
281 lines (221 loc) · 8.15 KB
/
Folder.vb
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System.Text
Imports Microsoft.Win32
Public Class Folder
Shared ReadOnly Property Desktop() As String
Get
Return Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
End Get
End Property
Shared ReadOnly Property Startup() As String
Get
Return Forms.Application.StartupPath
End Get
End Property
Shared ReadOnly Property FontSystem As String
Get
Return Environment.GetFolderPath(Environment.SpecialFolder.Fonts)
End Get
End Property
Shared ReadOnly Property Current() As String
Get
Return Environment.CurrentDirectory
End Get
End Property
Shared ReadOnly Property Programs() As String
Get
Return GetFolderPath(Environment.SpecialFolder.ProgramFiles).FixDir
End Get
End Property
Shared ReadOnly Property AppDataCommon() As String
Get
Return GetFolderPath(Environment.SpecialFolder.CommonApplicationData).FixDir
End Get
End Property
Shared ReadOnly Property AppDataLocal() As String
Get
Return GetFolderPath(Environment.SpecialFolder.LocalApplicationData).FixDir
End Get
End Property
Shared ReadOnly Property AppDataRoaming() As String
Get
Return GetFolderPath(Environment.SpecialFolder.ApplicationData).FixDir
End Get
End Property
Shared ReadOnly Property Windows() As String
Get
Return GetFolderPath(Environment.SpecialFolder.Windows).FixDir
End Get
End Property
Shared ReadOnly Property Temp() As String
Get
Return Path.GetTempPath
End Get
End Property
Shared ReadOnly Property Apps As String
Get
Return Startup + "\Apps"
End Get
End Property
Shared ReadOnly Property Plugins As String
Get
Return Apps + "\Plugins"
End Get
End Property
Shared ReadOnly Property Fonts As String
Get
Return Apps + "\Fonts"
End Get
End Property
<DllImport("shfolder.dll", CharSet:=CharSet.Unicode)>
Private Shared Function SHGetFolderPath(hwndOwner As IntPtr, nFolder As Integer, hToken As IntPtr, dwFlags As Integer, lpszPath As StringBuilder) As Integer
End Function
Private Shared Function GetFolderPath(folder As Environment.SpecialFolder) As String
Dim sb As New StringBuilder(260)
SHGetFolderPath(IntPtr.Zero, CInt(folder), IntPtr.Zero, 0, sb)
Dim ret = sb.ToString.FixDir '.NET fails on 'D:'
Call New FileIOPermission(FileIOPermissionAccess.PathDiscovery, ret).Demand()
Return ret
End Function
End Class
Public Class PathBase
Shared ReadOnly Property Separator() As Char
Get
Return Path.DirectorySeparatorChar
End Get
End Property
Shared Function IsSameBase(a As String, b As String) As Boolean
Return FilePath.GetBase(a).EqualIgnoreCase(FilePath.GetBase(b))
End Function
Shared Function IsSameDir(a As String, b As String) As Boolean
Return FilePath.GetDir(a).EqualIgnoreCase(FilePath.GetDir(b))
End Function
Shared Function IsValidFileSystemName(name As String) As Boolean
If name = "" Then Return False
Dim chars = """*/:<>?\|^".ToCharArray
For Each i In name.ToCharArray
If chars.Contains(i) Then Return False
If Convert.ToInt32(i) < 32 Then Return False
Next
Return True
End Function
Shared Function RemoveIllegalCharsFromName(name As String) As String
If name = "" Then Return ""
Dim chars = """*/:<>?\|^".ToCharArray
For Each i In name.ToCharArray
If chars.Contains(i) Then
name = name.Replace(i, "_")
End If
Next
For x = 1 To 31
If name.Contains(Convert.ToChar(x)) Then
name = name.Replace(Convert.ToChar(x), "_"c)
End If
Next
Return name
End Function
End Class
Public Class DirPath
Inherits PathBase
Shared Function TrimTrailingSeparator(path As String) As String
If path = "" Then Return ""
If path.EndsWith(Separator) AndAlso Not path.Length <= 3 Then
Return path.TrimEnd(Separator)
End If
Return path
End Function
Shared Function FixSeperator(path As String) As String
If path.Contains("\") AndAlso Separator <> "\" Then
path = path.Replace("\", Separator)
End If
If path.Contains("/") AndAlso Separator <> "/" Then
path = path.Replace("/", Separator)
End If
Return path
End Function
Shared Function GetParent(path As String) As String
If path = "" Then Return ""
Dim temp = TrimTrailingSeparator(path)
If temp.Contains(Separator) Then path = temp.LeftLast(Separator) + Separator
Return path
End Function
Shared Function GetName(path As String) As String
If path = "" Then Return ""
path = TrimTrailingSeparator(path)
Return path.RightLast(Separator)
End Function
Shared Function IsInSysDir(path As String) As Boolean
If path = "" Then Return False
If Not path.EndsWith("\") Then path += "\"
Return path.ToUpper.Contains(Folder.Programs.ToUpper)
End Function
Shared Function IsFixedDrive(path As String) As Boolean
Try
If path <> "" Then Return New DriveInfo(path).DriveType = DriveType.Fixed
Catch ex As Exception
End Try
End Function
End Class
Public Class FilePath
Inherits PathBase
Private Value As String
Sub New(path As String)
Value = path
End Sub
Shared Function GetDir(path As String) As String
If path = "" Then Return ""
If path.Contains("\") Then path = path.LeftLast("\") + "\"
Return path
End Function
Shared Function GetDirAndBase(path As String) As String
Return GetDir(path) + GetBase(path)
End Function
Shared Function GetName(path As String) As String
If Not path Is Nothing Then
Dim index = path.LastIndexOf(IO.Path.DirectorySeparatorChar)
If index > -1 Then
Return path.Substring(index + 1)
End If
End If
Return path
End Function
Shared Function GetExtFull(filepath As String) As String
Return GetExt(filepath, True)
End Function
Shared Function GetExt(filepath As String) As String
Return GetExt(filepath, False)
End Function
Shared Function GetExt(filepath As String, dot As Boolean) As String
If filepath = "" Then Return ""
Dim chars = filepath.ToCharArray
For x = filepath.Length - 1 To 0 Step -1
If chars(x) = Separator Then Return ""
If chars(x) = "."c Then Return filepath.Substring(x + If(dot, 0, 1)).ToLower
Next
Return ""
End Function
Shared Function GetDirNoSep(path As String) As String
path = GetDir(path)
If path.EndsWith(Separator) Then path = TrimSep(path)
Return path
End Function
Shared Function GetBase(path As String) As String
If path = "" Then Return ""
Dim ret = path
If ret.Contains(Separator) Then ret = ret.RightLast(Separator)
If ret.Contains(".") Then ret = ret.LeftLast(".")
Return ret
End Function
Shared Function TrimSep(path As String) As String
If path = "" Then Return ""
If path.EndsWith(Separator) AndAlso Not path.EndsWith(":" + Separator) Then
Return path.TrimEnd(Separator)
End If
Return path
End Function
Shared Function GetDirNameOnly(path As String) As String
Return FilePath.GetDirNoSep(path).RightLast("\")
End Function
End Class