}// ~public MBRScan( string physicalDriveName )----------------------------------------- public bool Scan(string physicalDriveName) { const uint SectorSize = 512; byte[] buff = new byte[512]; uint nBytesRead; // Create handle to physical drive SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0); // - - - - - - - - - - - - main MBR - - - - - - - - - - - - - - - - - - - - WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0); Mbr = new MBR(buff); if (Mbr.isProtective()) { return(true); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ~mainMBR shFile.Close(); //add to Sections primary sections for (int sectionNumber = 0; sectionNumber < 4; sectionNumber++) { if (Mbr[sectionNumber].Type == 0xF || Mbr[sectionNumber].Type == 0x5) { ulong baseAddr = Mbr[sectionNumber].getFirstSector(); EbrScan(physicalDriveName, baseAddr, 0); } else { Sections.Add(Mbr[sectionNumber]); } } return(false); }// -------------------------------------------------------------------- ~Scan()
//ISystemic //тип сенгментації //-------------------------------------------------------Constructor(string)------------------ public PhysicalDrive(string driveName) { const uint SectorSize = 512; byte[] buff = new byte[512]; uint nBytesRead; name = driveName; SafeFileHandle shFile = WinApi.CreateFile(driveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0); if (shFile.IsInvalid) { throw new DriveNotFoundException("Drive" + driveName + "is not avalible"); } WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0); if (shFile.IsInvalid) { throw new InvalidDataException("Reading data form " + driveName + " is not avalible"); } //--------------------------------------------------- MBR protectiveMbr = new MBR(buff); if (protectiveMbr.isProtective()) { Scanner = new GPTScan(name); } else { Scanner = new MBRScan(name); } //--------------------------------------------------- //--------------------------------------------------- for (int i = 0; i < Scanner.Length; i++) { volumes.Add(new Volume(driveName, Scanner[i])); } //--------------------------------------------------- shFile.Close(); }
public MBRScan(string physicalDriveName) { const uint SectorSize = 512; byte[] buff = new byte[512]; uint nBytesRead; // Create handle to physical drive SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0); if (shFile.IsInvalid) { throw new DriveNotFoundException(physicalDriveName + " not found"); } // - - - - - - - - - - - - main MBR - - - - - - - - - - - - - - - - - - - - WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0); Mbr = new MBR(buff); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ~mainMBR shFile.Close(); //add to Sections primary sections for (int sectionNumber = 0; sectionNumber < 4; sectionNumber++) { if (Mbr[sectionNumber].Type == 0xF || Mbr[sectionNumber].Type == 0x5) { ulong baseAddr = Mbr[sectionNumber].getFirstSector(); EbrScan(physicalDriveName, baseAddr, 0); } else { Sections.Add(Mbr[sectionNumber]); } } }// ~public MBRScan( string physicalDriveName )-----------------------------------------
public GPTScan(string physicalDriveName) { const uint SectorSize = 512; byte[] buff = new byte[512]; uint nBytesRead; SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0); //LBA0 WinApi.ReadFile(shFile, buff, 512, out nBytesRead, (IntPtr)0); //LBA1 ---------------------------HEADER---------------------------------------------- Mbr = new MBR(buff); UInt64 addr = 512; int hPart = (int)(addr >> 32); uint lPart = (uint)addr; WinApi.SetFilePointer(shFile, lPart, out hPart, (uint)SeekOrigin.Begin); if (shFile.IsInvalid) { throw new InvalidDataException(); } try { WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0); } catch (FileLoadException ex) { ex.ToString();//!!!!!!!!!!!!!!!!! shit } Header = new GPTHeader(buff); //---------------------------------------------------------------------------- ~Header //LBA2...X for (uint i = 0; i < Header.getNumberOfPartitionEntries() / (SectorSize / Header.getSizeOfPartitionEntry()); i++) { addr += 512; hPart = (int)(addr >> 32); lPart = (uint)addr; WinApi.SetFilePointer(shFile, lPart, out hPart, (uint)SeekOrigin.Begin); WinApi.ReadFile(shFile, buff, 512, out nBytesRead, (IntPtr)0); int offset = 0;//офсет внутри сектора for (int j = 0; j < 512 / Header.getSizeOfPartitionEntry(); j++) { //---Анализируем гуид на предмет неиспользуемой записи byte[] guidBuff = new byte [16]; for (int guidIndex = 0; guidIndex < 16; guidIndex++) { guidBuff[guidIndex] = buff[guidIndex + offset]; } Guid guid = new Guid(guidBuff); //---------------------------------------------------------- if (!guid.Equals(Guid.Empty)) { Sections.Add(new GPTSection(buff, offset)); } offset += (int)Header.getSizeOfPartitionEntry(); } } shFile.Close(); }//GPTScan( string PhysicalDriveName )