wasSharpNET – Diff between revs 22 and 27
?pathlinks?
Rev 22 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
- | 6 | |
||
- | 7 | using System; |
||
6 | |
8 | using System.Diagnostics; |
|
7 | using System.IO; |
9 | using System.IO; |
|
8 | |
10 | |
|
9 | namespace wasSharpNET.IO.Utilities |
11 | namespace wasSharpNET.IO.Utilities |
|
10 | { |
12 | { |
|
11 | public static class IOExtensions |
13 | public static class IOExtensions |
|
12 | { |
14 | { |
|
- | 15 | /// <summary> |
||
- | 16 | /// Attempt to obtain a filestream by polling a file till the handle becomes available. |
||
- | 17 | /// </summary> |
||
- | 18 | /// <param name="path">the path to the file</param> |
||
- | 19 | /// <param name="mode">the file mode to use</param> |
||
- | 20 | /// <param name="access">the level of access to the file</param> |
||
- | 21 | /// <param name="share">the type of file share locking</param> |
||
- | 22 | /// <param name="timeout">the timeout in milliseconds to fail opening the file</param> |
||
- | 23 | /// <returns>a filestream if the file was opened successfully</returns> |
||
- | 24 | public static FileStream GetWriteStream(string path, FileMode mode, FileAccess access, FileShare share, |
||
- | 25 | int timeout) |
||
- | 26 | { |
||
- | 27 | var time = Stopwatch.StartNew(); |
||
- | 28 | while (time.ElapsedMilliseconds < timeout) |
||
- | 29 | try |
||
- | 30 | { |
||
- | 31 | return new FileStream(path, mode, access, share); |
||
- | 32 | } |
||
- | 33 | catch (IOException e) |
||
- | 34 | { |
||
- | 35 | // access error |
||
- | 36 | if (e.HResult != -2147024864) |
||
- | 37 | throw; |
||
- | 38 | } |
||
- | 39 | |
||
- | 40 | throw new TimeoutException($"Failed to get a write handle to {path} within {timeout}ms."); |
||
- | 41 | } |
||
- | 42 | |
||
13 | public static bool isRootedIn(this string path, string root) |
43 | public static bool isRootedIn(this string path, string root) |
|
14 | { |
44 | { |
|
15 | // Path is empty and root is empty. |
45 | // Path is empty and root is empty. |
|
16 | if (string.IsNullOrEmpty(path) && string.IsNullOrEmpty(root)) |
46 | if (string.IsNullOrEmpty(path) && string.IsNullOrEmpty(root)) |
|
17 | return true; |
47 | return true; |
|
18 | |
48 | |
|
19 | // Path is empty but the root path is not empty. |
49 | // Path is empty but the root path is not empty. |
|
20 | if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(root)) |
50 | if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(root)) |
|
21 | return true; |
51 | return true; |
|
22 | |
52 | |
|
23 | // The supplied root path is empty. |
53 | // The supplied root path is empty. |
|
24 | if (string.IsNullOrEmpty(root)) |
54 | if (string.IsNullOrEmpty(root)) |
|
25 | return false; |
55 | return false; |
|
26 | |
56 | |
|
27 | // The path is empty and the root is not. |
57 | // The path is empty and the root is not. |
|
28 | if (string.IsNullOrEmpty(path)) |
58 | if (string.IsNullOrEmpty(path)) |
|
29 | return true; |
59 | return true; |
|
30 | |
60 | |
|
31 | DirectoryInfo p = new DirectoryInfo(path); |
61 | var p = new DirectoryInfo(path); |
|
32 | DirectoryInfo r = new DirectoryInfo(root); |
62 | var r = new DirectoryInfo(root); |
|
33 | |
63 | |
|
34 | return string.Equals(p.Parent?.FullName, r.Parent?.FullName) || isRootedIn(p.Parent?.FullName, root); |
64 | return string.Equals(p.Parent?.FullName, r.Parent?.FullName) || isRootedIn(p.Parent?.FullName, root); |
|
35 | } |
65 | } |
|
36 | |
66 | |
|
37 | public static void Empty(this string directory) |
67 | public static void Empty(this string directory) |
|
38 | { |
68 | { |
|
39 | DirectoryInfo dir = new DirectoryInfo(directory); |
69 | var dir = new DirectoryInfo(directory); |
|
40 | |
70 | |
|
41 | foreach (FileInfo fi in dir.GetFiles()) |
- | ||
42 | { |
71 | foreach (var fi in dir.GetFiles()) |
|
43 | fi.Delete(); |
- | ||
44 | } |
72 | fi.Delete(); |
|
45 | |
73 | |
|
46 | foreach (DirectoryInfo di in dir.GetDirectories()) |
74 | foreach (var di in dir.GetDirectories()) |
|
47 | { |
75 | { |
|
48 | Empty(di.FullName); |
76 | Empty(di.FullName); |
|
49 | di.Delete(); |
77 | di.Delete(); |
|
50 | } |
78 | } |
|
51 | } |
79 | } |
|
52 | } |
80 | } |
|
53 | } |
81 | } |
|
54 | |
82 | |
|
- | 83 |
|
||
- | 84 | |
||
- | 85 | |
||
- | 86 | |