Skip to content

Commit 9d018c8

Browse files
committed
完善目录和文件信息的扩展方法。 👀
1 parent 251c926 commit 9d018c8

3 files changed

Lines changed: 69 additions & 4 deletions

File tree

Zongsoft.Core/src/IO/DirectoryInfo.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Authors:
1010
* 钟峰(Popeye Zhong) <zongsoft@qq.com>
1111
*
12-
* Copyright (C) 2010-2020 Zongsoft Studio <http://www.zongsoft.com>
12+
* Copyright (C) 2010-2025 Zongsoft Studio <http://www.zongsoft.com>
1313
*
1414
* This file is part of Zongsoft.Core library.
1515
*
@@ -28,12 +28,14 @@
2828
*/
2929

3030
using System;
31+
using System.Threading;
32+
using System.Threading.Tasks;
3133
using System.Collections.Generic;
3234

3335
namespace Zongsoft.IO;
3436

3537
[Serializable]
36-
public class DirectoryInfo : PathInfo
38+
public partial class DirectoryInfo : PathInfo
3739
{
3840
#region 构造函数
3941
protected DirectoryInfo() { }
@@ -48,3 +50,36 @@ public DirectoryInfo(Path path, DateTime? createdTime, DateTime? modifiedTime, I
4850
public override bool IsDirectory => true;
4951
#endregion
5052
}
53+
54+
partial class DirectoryInfo
55+
{
56+
public bool Delete() => FileSystem.Directory.Delete(this.Url);
57+
public ValueTask<bool> DeleteAsync(CancellationToken cancellation = default) => FileSystem.Directory.DeleteAsync(this.Url, cancellation);
58+
59+
public void Move(string destination) => FileSystem.Directory.Move(this.Url, destination);
60+
public ValueTask MoveAsync(string destination, CancellationToken cancellation = default) => FileSystem.Directory.MoveAsync(this.Url, destination, cancellation);
61+
62+
public bool Exists() => FileSystem.Directory.Exists(this.Url);
63+
public ValueTask<bool> ExistsAsync(CancellationToken cancellation = default) => FileSystem.Directory.ExistsAsync(this.Url, cancellation);
64+
65+
public IEnumerable<PathInfo> GetChildren() => FileSystem.Directory.GetChildren(this.Url);
66+
public IEnumerable<PathInfo> GetChildren(string pattern, bool recursive = false) => FileSystem.Directory.GetChildren(this.Url, pattern, recursive);
67+
68+
public IAsyncEnumerable<PathInfo> GetChildrenAsync(CancellationToken cancellation = default) => FileSystem.Directory.GetChildrenAsync(this.Url, cancellation);
69+
public IAsyncEnumerable<PathInfo> GetChildrenAsync(string pattern, CancellationToken cancellation = default) => FileSystem.Directory.GetChildrenAsync(this.Url, pattern, cancellation);
70+
public IAsyncEnumerable<PathInfo> GetChildrenAsync(string pattern, bool recursive, CancellationToken cancellation = default) => FileSystem.Directory.GetChildrenAsync(this.Url, pattern, recursive, cancellation);
71+
72+
public IEnumerable<DirectoryInfo> GetDirectories() => FileSystem.Directory.GetDirectories(this.Url);
73+
public IEnumerable<DirectoryInfo> GetDirectories(string pattern, bool recursive = false) => FileSystem.Directory.GetDirectories(this.Url, pattern, recursive);
74+
75+
public IAsyncEnumerable<DirectoryInfo> GetDirectoriesAsync(CancellationToken cancellation = default) => FileSystem.Directory.GetDirectoriesAsync(this.Url, cancellation);
76+
public IAsyncEnumerable<DirectoryInfo> GetDirectoriesAsync(string pattern, CancellationToken cancellation = default) => FileSystem.Directory.GetDirectoriesAsync(this.Url, pattern, cancellation);
77+
public IAsyncEnumerable<DirectoryInfo> GetDirectoriesAsync(string pattern, bool recursive, CancellationToken cancellation = default) => FileSystem.Directory.GetDirectoriesAsync(this.Url, pattern, recursive, cancellation);
78+
79+
public IEnumerable<FileInfo> GetFiles() => FileSystem.Directory.GetFiles(this.Url);
80+
public IEnumerable<FileInfo> GetFiles(string pattern, bool recursive = false) => FileSystem.Directory.GetFiles(this.Url, pattern, recursive);
81+
82+
public IAsyncEnumerable<FileInfo> GetFilesAsync(CancellationToken cancellation = default) => FileSystem.Directory.GetFilesAsync(this.Url, cancellation);
83+
public IAsyncEnumerable<FileInfo> GetFilesAsync(string pattern, CancellationToken cancellation = default) => FileSystem.Directory.GetFilesAsync(this.Url, pattern, cancellation);
84+
public IAsyncEnumerable<FileInfo> GetFilesAsync(string pattern, bool recursive, CancellationToken cancellation = default) => FileSystem.Directory.GetFilesAsync(this.Url, pattern, recursive, cancellation);
85+
}

Zongsoft.Core/src/IO/FileInfo.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Authors:
1010
* 钟峰(Popeye Zhong) <zongsoft@qq.com>
1111
*
12-
* Copyright (C) 2010-2020 Zongsoft Studio <http://www.zongsoft.com>
12+
* Copyright (C) 2010-2025 Zongsoft Studio <http://www.zongsoft.com>
1313
*
1414
* This file is part of Zongsoft.Core library.
1515
*
@@ -28,12 +28,14 @@
2828
*/
2929

3030
using System;
31+
using System.Threading;
32+
using System.Threading.Tasks;
3133
using System.Collections.Generic;
3234

3335
namespace Zongsoft.IO;
3436

3537
[Serializable]
36-
public class FileInfo : PathInfo, IEquatable<FileInfo>
38+
public partial class FileInfo : PathInfo, IEquatable<FileInfo>
3739
{
3840
#region 成员字段
3941
private long _size;
@@ -99,3 +101,30 @@ public string Type
99101
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _size);
100102
#endregion
101103
}
104+
105+
partial class FileInfo
106+
{
107+
public bool Delete() => FileSystem.File.Delete(this.Url);
108+
public ValueTask<bool> DeleteAsync(CancellationToken cancellation = default) => FileSystem.File.DeleteAsync(this.Url, cancellation);
109+
110+
public bool Exists() => FileSystem.File.Exists(this.Url);
111+
public ValueTask<bool> ExistsAsync(CancellationToken cancellation = default) => FileSystem.File.ExistsAsync(this.Url, cancellation);
112+
113+
public void Copy(string destination, bool overwrite = true) => FileSystem.File.Copy(this.Url, destination, overwrite);
114+
public ValueTask CopyAsync(string destination, CancellationToken cancellation = default) => FileSystem.File.CopyAsync(this.Url, destination, cancellation);
115+
public ValueTask CopyAsync(string destination, bool overwrite, CancellationToken cancellation = default) => FileSystem.File.CopyAsync(this.Url, destination, overwrite, cancellation);
116+
117+
public void Move(string destination) => FileSystem.File.Move(this.Url, destination);
118+
public ValueTask MoveAsync(string destination, CancellationToken cancellation = default) => FileSystem.File.MoveAsync(this.Url, destination, cancellation);
119+
120+
public System.IO.Stream Open(System.IO.FileMode mode, IEnumerable<KeyValuePair<string, string>> properties = null) => FileSystem.File.Open(this.Url, mode, properties);
121+
public System.IO.Stream Open(System.IO.FileMode mode, System.IO.FileAccess access, IEnumerable<KeyValuePair<string, string>> properties = null) => FileSystem.File.Open(this.Url, mode, access, properties);
122+
public System.IO.Stream Open(System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, IEnumerable<KeyValuePair<string, string>> properties = null) => FileSystem.File.Open(this.Url, mode, access, share, properties);
123+
124+
public ValueTask<System.IO.Stream> OpenAsync(System.IO.FileMode mode, CancellationToken cancellation = default) => FileSystem.File.OpenAsync(this.Url, mode, cancellation);
125+
public ValueTask<System.IO.Stream> OpenAsync(System.IO.FileMode mode, IEnumerable<KeyValuePair<string, string>> properties, CancellationToken cancellation = default) => FileSystem.File.OpenAsync(this.Url, mode, properties, cancellation);
126+
public ValueTask<System.IO.Stream> OpenAsync(System.IO.FileMode mode, System.IO.FileAccess access, CancellationToken cancellation = default) => FileSystem.File.OpenAsync(this.Url, mode, access, cancellation);
127+
public ValueTask<System.IO.Stream> OpenAsync(System.IO.FileMode mode, System.IO.FileAccess access, IEnumerable<KeyValuePair<string, string>> properties, CancellationToken cancellation = default) => FileSystem.File.OpenAsync(this.Url, mode, access, properties, cancellation);
128+
public ValueTask<System.IO.Stream> OpenAsync(System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, CancellationToken cancellation = default) => FileSystem.File.OpenAsync(this.Url, mode, access, share, cancellation);
129+
public ValueTask<System.IO.Stream> OpenAsync(System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, IEnumerable<KeyValuePair<string, string>> properties, CancellationToken cancellation = default) => FileSystem.File.OpenAsync(this.Url, mode, access, share, properties, cancellation);
130+
}

Zongsoft.Core/src/IO/Path.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public bool Equals(Path path)
159159
#region 符号重写
160160
public static bool operator ==(Path left, Path right) => left.Equals(right);
161161
public static bool operator !=(Path left, Path right) => !(left == right);
162+
public static implicit operator string(Path path) => path.Url;
162163
#endregion
163164

164165
#region 静态方法

0 commit comments

Comments
 (0)