wasSharpNET – Diff between revs 22 and 27
?pathlinks?
Rev 22 | Rev 27 | |||
---|---|---|---|---|
Line 2... | Line 2... | |||
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 | /////////////////////////////////////////////////////////////////////////// |
|
Line -... | Line 6... | |||
- | 6 | |
||
- | 7 | using System; |
||
6 | |
8 | using System.Diagnostics; |
|
Line 7... | Line 9... | |||
7 | using System.IO; |
9 | using System.IO; |
|
8 | |
10 | |
|
9 | namespace wasSharpNET.IO.Utilities |
11 | namespace wasSharpNET.IO.Utilities |
|
10 | { |
12 | { |
|
- | 13 | public static class IOExtensions |
||
- | 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."); |
||
11 | public static class IOExtensions |
41 | } |
|
12 | { |
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. |
|
Line 26... | Line 56... | |||
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)) |
|
Line 29... | Line 59... | |||
29 | return true; |
59 | return true; |
|
30 | |
60 | |
|
Line 31... | Line 61... | |||
31 | DirectoryInfo p = new DirectoryInfo(path); |
61 | var p = new DirectoryInfo(path); |
|
32 | DirectoryInfo r = new DirectoryInfo(root); |
62 | var r = new DirectoryInfo(root); |
|
Line 33... | Line 63... | |||
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 | } |
|
Line 36... | Line 66... | |||
36 | |
66 | |
|
37 | public static void Empty(this string directory) |
- | ||
38 | { |
67 | public static void Empty(this string directory) |
|
39 | DirectoryInfo dir = new DirectoryInfo(directory); |
- | ||
Line 40... | Line 68... | |||
40 | |
68 | { |
|
41 | foreach (FileInfo fi in dir.GetFiles()) |
69 | var dir = new DirectoryInfo(directory); |
|
42 | { |
70 | |
|
43 | fi.Delete(); |
71 | foreach (var fi in dir.GetFiles()) |
|
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); |