/
DriveInfoWrapper.cs
165 lines (152 loc) · 7.95 KB
/
DriveInfoWrapper.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
using System.Runtime.Versioning;
namespace System.IO.Abstractions
{
/// <summary>
/// The wrapper for a <see cref="DriveInfo"/>.
/// </summary>
[Serializable]
public class DriveInfoWrapper : DriveInfoBase
{
/// <summary>
/// The instance of the real <see cref="FileSystem"/>.
/// </summary>
private readonly DriveInfo instance;
/// <summary>
/// Initializes a new instance of the <see cref="DriveInfoWrapper"/> class, which acts as a wrapper for a drive info.
/// </summary>
/// <param name="fileSystem">The underlying IFileSystem.</param>
/// <param name="instance">The drive info.</param>
public DriveInfoWrapper(IFileSystem fileSystem, DriveInfo instance) : base(fileSystem)
{
this.instance = instance ?? throw new ArgumentNullException(nameof(instance));
}
/// <summary>
/// Gets or sets the name of a drive, such as C:\.
/// </summary>
/// <value>The name of the drive.</value>
/// <remarks>
/// This property is the name assigned to the drive, such as C:\ or E:\.
/// </remarks>
public override string Name
{
get { return instance.Name; }
}
/// <summary>
/// Gets or sets the drive type, such as CD-ROM, removable, network, or fixed.
/// </summary>
/// <value>One of the enumeration values that specifies a drive type.</value>
/// <remarks>
/// The DriveType property indicates whether a drive is one of the following: CDRom, Fixed, Network, NoRootDirectory, Ram, Removable, or Unknown.
/// These values are described in the DriveType enumeration.
/// </remarks>
public override DriveType DriveType
{
get { return instance.DriveType; }
}
/// <summary>
/// Gets or sets the name of the file system, such as NTFS or FAT32.
/// </summary>
/// <remarks>
/// Use DriveFormat to determine what formatting a drive uses.
/// </remarks>
/// <value>The name of the file system on the specified drive.</value>
/// <exception cref="UnauthorizedAccessException">Thrown if the access to the drive information is denied.</exception>
/// <exception cref="DriveNotFoundException">Thrown if the drive does not exist or is not mapped.</exception>
/// <exception cref="IOException">Thrown if an I/O error occurred (for example, a disk error or a drive was not ready).</exception>
public override string DriveFormat
{
get { return instance.DriveFormat; }
}
/// <summary>
/// Gets or sets a value indicating whether a drive is ready.
/// </summary>
/// <value>
/// <see langword="true"/> if the drive is ready; <see langword="false"/> if the drive is not ready.
/// </value>
/// <remarks>
/// IsReady indicates whether a drive is ready.
/// For example, it indicates whether a CD is in a CD drive or whether a removable storage device is ready for read/write operations.
/// If you do not test whether a drive is ready, and it is not ready, querying the drive using <see cref="DriveInfoBase"/> will raise an IOException.
/// Do not rely on IsReady to avoid catching exceptions from other members such as TotalSize, TotalFreeSpace, and <see cref="DriveInfoBase.DriveFormat"/>.
/// Between the time that your code checks IsReady and then accesses one of the other properties (even if the access occurs immediately after the check),
/// a drive may have been disconnected or a disk may have been removed.
/// </remarks>
public override bool IsReady
{
get { return instance.IsReady; }
}
/// <summary>
/// Gets or sets the amount of available free space on a drive, in bytes.
/// </summary>
/// <value>The amount of free space available on the drive, in bytes.</value>
/// <remarks>
/// This property indicates the amount of free space available on the drive.
/// Note that this number may be different from the TotalFreeSpace number because this property takes into account disk quotas.
/// </remarks>
/// <exception cref="UnauthorizedAccessException">Thrown if the access to the drive information is denied.</exception>
/// <exception cref="IOException">Thrown if an I/O error occurred (for example, a disk error or a drive was not ready).</exception>
public override long AvailableFreeSpace
{
get { return instance.AvailableFreeSpace; }
}
/// <summary>
/// Gets or sets the total amount of free space available on a drive, in bytes.
/// </summary>
/// <value>The total free space available on a drive, in bytes.</value>
/// <remarks>This property indicates the total amount of free space available on the drive, not just what is available to the current user.</remarks>
/// <exception cref="UnauthorizedAccessException">Thrown if the access to the drive information is denied.</exception>
/// <exception cref="DriveNotFoundException">Thrown if the drive does not exist or is not mapped.</exception>
/// <exception cref="IOException">Thrown if an I/O error occurred (for example, a disk error or a drive was not ready).</exception>
public override long TotalFreeSpace
{
get { return instance.TotalFreeSpace; }
}
/// <summary>
/// Gets or sets the total size of storage space on a drive, in bytes.
/// </summary>
/// <value>The total size of the drive, in bytes.</value>
/// <remarks>
/// This property indicates the total size of the drive in bytes, not just what is available to the current user.
/// </remarks>
/// <exception cref="UnauthorizedAccessException">Thrown if the access to the drive information is denied.</exception>
/// <exception cref="DriveNotFoundException">Thrown if the drive does not exist or is not mapped.</exception>
/// <exception cref="IOException">Thrown if an I/O error occurred (for example, a disk error or a drive was not ready).</exception>
public override long TotalSize
{
get { return instance.TotalSize; }
}
/// <summary>
/// Gets or sets the root directory of a drive.
/// </summary>
/// <value>An object that contains the root directory of the drive.</value>
public override IDirectoryInfo RootDirectory
{
get { return new DirectoryInfoWrapper(FileSystem, instance.RootDirectory); }
}
/// <summary>
/// Gets or sets the volume label of a drive.
/// </summary>
/// <value>The volume label.</value>
/// <remarks>
/// The label length is determined by the operating system. For example, NTFS allows a volume label to be up to 32 characters long. Note that <see langword="null"/> is a valid VolumeLabel.
/// </remarks>
/// <exception cref="IOException">Thrown if an I/O error occurred (for example, a disk error or a drive was not ready).</exception>
/// <exception cref="DriveNotFoundException">Thrown if the drive does not exist or is not mapped.</exception>
/// <exception cref="System.Security.SecurityException">Thrown if the caller does not have the required permission.</exception>
/// <exception cref="UnauthorizedAccessException">
/// Thrown if the volume label is being set on a network or CD-ROM drive
/// -or-
/// Access to the drive information is denied.
/// </exception>
public override string VolumeLabel
{
get { return instance.VolumeLabel; }
[SupportedOSPlatform("windows")]
set { instance.VolumeLabel = value; }
}
public override string ToString()
{
return instance.ToString();
}
}
}