From de7c24758345da92f730605da58e855c8bf259bd Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 8 Oct 2020 12:26:28 -0700 Subject: [PATCH] Add initial version of sector reading to library --- DICUI.Library/Utilities/Drive.cs | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/DICUI.Library/Utilities/Drive.cs b/DICUI.Library/Utilities/Drive.cs index 594a99cd..183539bd 100644 --- a/DICUI.Library/Utilities/Drive.cs +++ b/DICUI.Library/Utilities/Drive.cs @@ -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; } + + /// + /// Read a sector from the given drive + /// + /// Sector number + /// Byte array representing the sector, null on error + 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; + } + } } }