Skip to content

Commit

Permalink
implemented support for DriveInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
manne committed Dec 8, 2015
1 parent fb69091 commit dc6bd5a
Show file tree
Hide file tree
Showing 22 changed files with 740 additions and 13 deletions.
2 changes: 1 addition & 1 deletion System.IO.Abstractions/DirectoryInfoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class DirectoryInfoBase : FileSystemInfoBase
public abstract void SetAccessControl(DirectorySecurity directorySecurity);
public abstract DirectoryInfoBase Parent { get; }
public abstract DirectoryInfoBase Root { get; }

public static implicit operator DirectoryInfoBase(DirectoryInfo directoryInfo)
{
if (directoryInfo == null)
Expand Down
5 changes: 5 additions & 0 deletions System.IO.Abstractions/DirectoryInfoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,10 @@ public override DirectoryInfoBase Root
{
get { return instance.Root; }
}

public override string ToString()
{
return instance.ToString();
}
}
}
124 changes: 124 additions & 0 deletions System.IO.Abstractions/DriveInfoBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
namespace System.IO.Abstractions
{
[Serializable]
public abstract class DriveInfoBase
{
/// <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 virtual long AvailableFreeSpace { get; protected set; }

/// <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 virtual string DriveFormat { get; protected set; }

/// <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 virtual DriveType DriveType { get; protected set; }

/// <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="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 virtual bool IsReady { get; protected set; }

/// <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 virtual string Name { get; protected set; }

/// <summary>
/// Gets or sets the root directory of a drive.
/// </summary>
/// <value>An object that contains the root directory of the drive.</value>
public virtual DirectoryInfoBase RootDirectory { get; protected set; }

/// <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 virtual long TotalFreeSpace { get; protected set; }

/// <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 virtual long TotalSize { get; protected set; }

/// <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 virtual string VolumeLabel { get; set; }

/// <summary>
/// Converts a <see cref="DriveInfo"/> into a <see cref="DriveInfoBase"/>.
/// </summary>
/// <param name="driveInfo">The drive info to be converted.</param>
public static implicit operator DriveInfoBase(DriveInfo driveInfo)
{
if (driveInfo == null)
{
return null;
}

return new DriveInfoWrapper(driveInfo);
}
}
}
23 changes: 23 additions & 0 deletions System.IO.Abstractions/DriveInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace System.IO.Abstractions
{
[Serializable]
internal class DriveInfoFactory : IDriveInfoFactory
{
/// <summary>
/// Retrieves the drive names of all logical drives on a computer.
/// </summary>
/// <returns>An array of type <see cref="DriveInfoBase"/> that represents the logical drives on a computer.</returns>
public DriveInfoBase[] GetDrives()
{
var driveInfos = DriveInfo.GetDrives();
var driveInfoWrappers = new DriveInfoBase[driveInfos.Length];
for (int index = 0; index < driveInfos.Length; index++)
{
var driveInfo = driveInfos[index];
driveInfoWrappers[index] = new DriveInfoWrapper(driveInfo);
}

return driveInfoWrappers;
}
}
}
167 changes: 167 additions & 0 deletions System.IO.Abstractions/DriveInfoWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
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="instance">The drive info.</param>
public DriveInfoWrapper(DriveInfo instance)
{
if (instance == null)
{
throw new ArgumentNullException("instance");
}

this.instance = 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 DirectoryInfoBase RootDirectory
{
get { return 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; }

set { instance.VolumeLabel = value; }
}

public override string ToString()
{
return instance.ToString();
}
}
}
7 changes: 7 additions & 0 deletions System.IO.Abstractions/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@ public IDirectoryInfoFactory DirectoryInfo
{
get { return directoryInfoFactory ?? (directoryInfoFactory = new DirectoryInfoFactory()); }
}

private readonly Lazy<DriveInfoFactory> driveInfoFactory = new Lazy<DriveInfoFactory>(() => new DriveInfoFactory());

public IDriveInfoFactory DriveInfo
{
get { return driveInfoFactory.Value; }
}
}
}
14 changes: 14 additions & 0 deletions System.IO.Abstractions/IDriveInfoFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace System.IO.Abstractions
{
/// <summary>
/// A factory to create all <see cref="DriveInfoBase"/> for a <see cref="IFileSystem"/>.
/// </summary>
public interface IDriveInfoFactory
{
/// <summary>
/// Retrieves the drive names of all logical drives on a computer.
/// </summary>
/// <returns>An array of type <see cref="DriveInfoBase"/> that represents the logical drives on a computer.</returns>
DriveInfoBase[] GetDrives();
}
}
1 change: 1 addition & 0 deletions System.IO.Abstractions/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IFileSystem
IFileInfoFactory FileInfo { get; }
PathBase Path { get; }
IDirectoryInfoFactory DirectoryInfo { get; }
IDriveInfoFactory DriveInfo { get; }
}
}
4 changes: 4 additions & 0 deletions System.IO.Abstractions/System.IO.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<Compile Include="DirectoryInfoFactory.cs" />
<Compile Include="DirectoryInfoWrapper.cs" />
<Compile Include="DirectoryWrapper.cs" />
<Compile Include="DriveInfoBase.cs" />
<Compile Include="DriveInfoFactory.cs" />
<Compile Include="DriveInfoWrapper.cs" />
<Compile Include="FileInfoBase.cs" />
<Compile Include="FileInfoFactory.cs" />
<Compile Include="FileInfoWrapper.cs" />
Expand All @@ -82,6 +85,7 @@
<Compile Include="FileWrapper.cs" />
<Compile Include="FileBase.cs" />
<Compile Include="IDirectoryInfoFactory.cs" />
<Compile Include="IDriveInfoFactory.cs" />
<Compile Include="IFileInfoFactory.cs" />
<Compile Include="IFileSystem.cs" />
<Compile Include="PathBase.cs" />
Expand Down
Loading

0 comments on commit dc6bd5a

Please sign in to comment.