Add initial version of sector reading to library

This commit is contained in:
Matt Nadareski
2020-10-08 12:26:28 -07:00
parent 997fd8f7ac
commit de7c247583

View File

@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using DICUI.Data;
namespace DICUI.Utilities
@@ -61,5 +62,39 @@ namespace DICUI.Utilities
this.InternalDriveType = driveType;
this.DriveInfo = driveInfo;
}
/// <summary>
/// Read a sector from the given drive
/// </summary>
/// <param name="sector">Sector number</param>
/// <returns>Byte array representing the sector, null on error</returns>
public byte[] ReadSector(long sector)
{
// Missing drive leter is not supported
if (string.IsNullOrEmpty(DriveInfo?.Name))
return null;
// We don't support negative sectors
if (sector < 0)
return null;
// Get the starting point
int sectorSize = 2048;
long start = sector * sectorSize;
// Try to read the sector from the device
try
{
var fs = File.OpenRead(@"\\?\E:");
fs.Seek(start, SeekOrigin.Begin);
byte[] buffer = new byte[sectorSize];
fs.Read(buffer, 0, sectorSize);
return buffer;
}
catch
{
return null;
}
}
}
}