-
Notifications
You must be signed in to change notification settings - Fork 1
/
PathExtensions.cs
245 lines (205 loc) · 6.02 KB
/
PathExtensions.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace Crestron.SimplSharp.CrestronIO
{
public static class PathEx
{
public static string GetTempFileName ()
{
FileStream f = null;
string path;
int count = 0;
var rnd = new Random ();
do
{
int num = rnd.Next ();
num++;
path = Path.Combine (GetTempPath (), "tmp" + num.ToString ("x") + ".tmp");
try
{
f = new FileStream (path, FileMode.CreateNew, (FileAccess)FileAccess.ReadWrite);
}
catch (IOException)
{
if (!File.Exists (path) || count++ > 65536)
throw;
}
catch (UnauthorizedAccessException ex)
{
if (count++ > 65536)
throw new IOException (ex.Message, ex);
}
} while (f == null);
f.Close ();
return path;
}
public static string GetTempPath ()
{
return String.Format ("\\Nvram\\App{0:D2}\\Temp\\{1}\\", InitialParametersClass.ApplicationNumber, InitialParametersClass.ProgramIDTag);
}
public static string GetFullPath (string path)
{
if (string.IsNullOrEmpty (path) || Path.IsPathRooted (path))
return path;
return Path.Combine (InitialParametersClass.ProgramDirectory.ToString (), path);
}
internal static bool IsDsc (char c)
{
return c == Path.DirectorySeparatorChar || c == Path.AltDirectorySeparatorChar;
}
internal static string DirectorySeparatorStr = Convert.ToString (Path.DirectorySeparatorChar);
public static string GetPathRoot (string path)
{
if (path == null)
return null;
if (path.Trim ().Length == 0)
throw new ArgumentException ("The specified path is not of a legal form.");
if (!Path.IsPathRooted (path))
return String.Empty;
if (Path.DirectorySeparatorChar == '/')
{
// UNIX
return IsDsc (path[0]) ? DirectorySeparatorStr : String.Empty;
}
else
{
// Windows
int len = 2;
if (path.Length == 1 && IsDsc (path[0]))
return DirectorySeparatorStr;
else if (path.Length < 2)
return String.Empty;
if (IsDsc (path[0]) && IsDsc (path[1]))
{
// UNC: \\server or \\server\share
// Get server
while (len < path.Length && !IsDsc (path[len]))
len++;
// Get share
if (len < path.Length)
{
len++;
while (len < path.Length && !IsDsc (path[len]))
len++;
}
return DirectorySeparatorStr +
DirectorySeparatorStr +
path.Substring (2, len - 2).Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}
else if (IsDsc (path[0]))
{
// path starts with '\' or '/'
return DirectorySeparatorStr;
}
else if (path[1] == Path.VolumeSeparatorChar)
{
// C:\folder
if (path.Length >= 3 && (IsDsc (path[2])))
len++;
}
else
return DirectorySeparatorStr;
return path.Substring (0, len);
}
}
public static string GetRandomFileName ()
{
// returns a 8.3 filename (total size 12)
StringBuilder sb = new StringBuilder (12);
// using strong crypto but without creating the file
//RandomNumberGenerator rng = RandomNumberGenerator.Create ();
var rng = new Random ();
byte[] buffer = new byte[11];
rng.NextBytes (buffer);
for (int i = 0; i < buffer.Length; i++)
{
if (sb.Length == 8)
sb.Append ('.');
// restrict to length of range [a..z0..9]
int b = (buffer[i] % 36);
char c = (char)(b < 26 ? (b + 'a') : (b - 26 + '0'));
sb.Append (c);
}
return sb.ToString ();
}
public static char[] GetInvalidFileNameChars ()
{
// return a new array as we do not want anyone to be able to change the values
return new char[41]
{
'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
'\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12',
'\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D',
'\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C', ':', '*', '?', '\\', '/'
};
}
public static string ChangeExtension (string path, string extension)
{
return Path.ChangeExtension (path, extension);
}
public static string Combine (string path1, string path2)
{
return Path.Combine (path1, path2);
}
public static string Combine (string path1, string path2, string path3)
{
return Path.Combine(Path.Combine (path1, path2), path3);
}
public static string Combine (string path1, string path2, string path3, string path4)
{
return Path.Combine (Path.Combine(Path.Combine (path1, path2), path3), path4);
}
public static string Combine (params string[] paths)
{
if (paths.Length == 0)
return String.Empty;
if (paths.Length == 1)
return paths[0];
var result = Path.Combine (paths[0], paths[1]);
if (paths.Length == 2)
return result;
for (int ix = 2; ix < paths.Length; ++ix)
result = Path.Combine (result, paths[ix]);
return result;
}
public static string GetDirectoryName (string path)
{
return Path.GetDirectoryName (path);
}
public static string GetExtension (string path)
{
return Path.GetExtension (path);
}
public static string GetFileName (string path)
{
return Path.GetFileName (path);
}
public static string GetFileNameWithoutExtension (string path)
{
return Path.GetFileNameWithoutExtension (path);
}
public static char[] GetInvalidPathChars ()
{
return Path.GetInvalidPathChars ();
}
public static bool HasExtension (string path)
{
return Path.HasExtension (path);
}
public static bool IsPathRooted (string path)
{
return Path.IsPathRooted (path);
}
public static long GetFreeSpace (string path)
{
return Path.GetFreeSpace (path);
}
public static readonly char AltDirectorySeparatorChar = Path.AltDirectorySeparatorChar;
public static readonly char DirectorySeparatorChar = Path.DirectorySeparatorChar;
public static readonly char PathSeparator = Path.PathSeparator;
public static readonly char VolumeSeparatorChar = Path.VolumeSeparatorChar;
}
}