-
Notifications
You must be signed in to change notification settings - Fork 795
/
FileSystemExtensions.cs
115 lines (105 loc) · 3.95 KB
/
FileSystemExtensions.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
namespace Serenity;
/// <summary>
/// IFileSystem extensions to reduce dependency on System.IO, in case these methods are also added to IFileSystem in the future
/// </summary>
public static class FileSystemExtensions
{
/// <summary>Changes the extension of a path string.</summary>
/// <param name="_">File system</param>
/// <param name="path">The path information to modify. The path cannot contain any of the characters
/// defined in System.IO.Path.GetInvalidPathChars.</param>
/// <param name="extension">The new extension (with or without a leading period). Specify null to remove
/// an existing extension from path.</param>
public static string ChangeExtension(this IFileSystem _, string path, string extension)
{
return System.IO.Path.ChangeExtension(path, extension);
}
/// <summary>
/// Combines path strings
/// </summary>
/// <param name="_">File system</param>
/// <param name="path1">Path 1</param>
/// <param name="path2">Path 2</param>
public static string Combine(this IFileSystem _, string path1, string path2)
{
return System.IO.Path.Combine(path1, path2);
}
/// <summary>
/// Combines path strings
/// </summary>
/// <param name="_">File system</param>
/// <param name="path1">Path 1</param>
/// <param name="path2">Path 2</param>
/// <param name="path3">Path 3</param>
public static string Combine(this IFileSystem _, string path1, string path2, string path3)
{
return System.IO.Path.Combine(path1, path2, path3);
}
/// <summary>
/// Combines path strings
/// </summary>
/// <param name="_">File system</param>
/// <param name="paths">Paths to combine</param>
public static string Combine(this IFileSystem _, params string[] paths)
{
return System.IO.Path.Combine(paths);
}
/// <summary>
/// Copies a file to another destination by optionally overwriting the destination if it exists
/// </summary>
/// <param name="fileSystem">File system</param>
/// <param name="sourceFileName">Source file name</param>
/// <param name="destFileName">Destination</param>
/// <param name="overwrite"></param>
public static void Copy(this IFileSystem fileSystem, string sourceFileName, string destFileName, bool overwrite)
{
using var sourceStream = fileSystem.OpenRead(sourceFileName);
using var destStream = fileSystem.CreateFile(destFileName, overwrite);
sourceStream.CopyTo(destStream);
}
/// <summary>
/// Gets directory name for path
/// </summary>
/// <param name="_">File system</param>
/// <param name="path">Path</param>
public static string GetDirectoryName(this IFileSystem _, string path)
{
return System.IO.Path.GetDirectoryName(path);
}
/// <summary>
/// Gets file name for path
/// </summary>
/// <param name="_">File system</param>
/// <param name="path">Path</param>
public static string GetFileName(this IFileSystem _, string path)
{
return System.IO.Path.GetFileName(path);
}
/// <summary>
/// Gets file name without extension for path
/// </summary>
/// <param name="_">File system</param>
/// <param name="path">Path</param>
public static string GetFileNameWithoutExtension(this IFileSystem _, string path)
{
return System.IO.Path.GetFileNameWithoutExtension(path);
}
/// <summary>
/// Gets extension for path
/// </summary>
/// <param name="_">File system</param>
/// <param name="path">Path</param>
public static string GetExtension(this IFileSystem _, string path)
{
return System.IO.Path.GetExtension(path);
}
/// <summary>
/// Gets if the path is rooted
/// </summary>
/// <param name="_">File system</param>
/// <param name="path">Path</param>
public static bool IsPathRooted(this IFileSystem _, string path)
{
return System.IO.Path.IsPathRooted(path);
}
}