用的下面这个程序
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Windows.Forms;
namespace Zgke
{
/// 磁盘扇区
///
public class DriverLoader
{
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
private System.IO.FileStream _DirverStream;
private long _SectorLength = 0;
private SafeFileHandle _DirverHandle;
///
/// 扇区数
///
public long SectorLength { get { return _SectorLength; } }
///
/// 获取磁盘扇区信息
///
/// G:
public DriverLoader(string DirverName)
{
if (DirverName == null && DirverName.Trim().Length == 0) return;
_DirverHandle = CreateFileA("\\\\.\\" + DirverName.Trim(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
_DirverStream = new System.IO.FileStream(_DirverHandle, System.IO.FileAccess.ReadWrite);
GetSectorCount();
} ///
/// 扇区显示转换
///
/// 扇区长度512
/// EB 52 90 ......55 AA
public string GetString(byte[] SectorBytes)
{
if (SectorBytes.Length != 512) return "";
StringBuilder ReturnText = new StringBuilder();
int RowCount = 0;
for (int i = 0; i != 512; i++)
{
ReturnText.Append(SectorBytes[i].ToString("X02") + " ");
if (RowCount == 15)
{
ReturnText.Append("\r\n");
RowCount = -1;
}
RowCount++;
}
return ReturnText.ToString();
}
///
/// 获取扇区数
///
private void GetSectorCount()
{
if (_DirverStream == null) return;
_DirverStream.Position = 0;
byte[] ReturnByte = new byte[512];
_DirverStream.Read(ReturnByte, 0, 512); //获取第1扇区
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x58) //DOS的好象都是32位
{
_SectorLength = (long)BitConverter.ToInt32(new byte[] { ReturnByte[32], ReturnByte[33], ReturnByte[34], ReturnByte[35] }, 0);
}
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x52) //NTFS好象是64位
{
_SectorLength = BitConverter.ToInt64(new byte[] { ReturnByte[40], ReturnByte[41], ReturnByte[42], ReturnByte[43], ReturnByte[44], ReturnByte[45], ReturnByte[46], ReturnByte[47] }, 0);
}
}
///
/// 读一个扇区
///
/// 扇区号
/// 如果扇区数字大于分区信息的扇区数返回NULL
public byte[] ReadSector(long SectorIndex)
{
if (SectorIndex > _SectorLength) return null;
_DirverStream.Position = SectorIndex * 512;
byte[] ReturnByte = new byte[512];
_DirverStream.Read(ReturnByte, 0, 512); //获取扇区
return ReturnByte;
}
///
/// 写入数据
///
/// 扇区长度512
/// 扇区位置
public void WritSector(byte[] SectorBytes, long SectorIndex)
{
if (SectorBytes.Length != 512) return;
if (SectorIndex > _SectorLength) return;
_DirverStream.Position = SectorIndex * 512;
_DirverStream.Write(SectorBytes, 0, 512); //写入扇区
}
///
/// 关闭
///
public void Close()
{
_DirverStream.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Windows.Forms;
namespace Zgke
{
/// 磁盘扇区
///
public class DriverLoader
{
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
private System.IO.FileStream _DirverStream;
private long _SectorLength = 0;
private SafeFileHandle _DirverHandle;
///
/// 扇区数
///
public long SectorLength { get { return _SectorLength; } }
///
/// 获取磁盘扇区信息
///
/// G:
public DriverLoader(string DirverName)
{
if (DirverName == null && DirverName.Trim().Length == 0) return;
_DirverHandle = CreateFileA("\\\\.\\" + DirverName.Trim(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
_DirverStream = new System.IO.FileStream(_DirverHandle, System.IO.FileAccess.ReadWrite);
GetSectorCount();
} ///
/// 扇区显示转换
///
/// 扇区长度512
/// EB 52 90 ......55 AA
public string GetString(byte[] SectorBytes)
{
if (SectorBytes.Length != 512) return "";
StringBuilder ReturnText = new StringBuilder();
int RowCount = 0;
for (int i = 0; i != 512; i++)
{
ReturnText.Append(SectorBytes[i].ToString("X02") + " ");
if (RowCount == 15)
{
ReturnText.Append("\r\n");
RowCount = -1;
}
RowCount++;
}
return ReturnText.ToString();
}
///
/// 获取扇区数
///
private void GetSectorCount()
{
if (_DirverStream == null) return;
_DirverStream.Position = 0;
byte[] ReturnByte = new byte[512];
_DirverStream.Read(ReturnByte, 0, 512); //获取第1扇区
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x58) //DOS的好象都是32位
{
_SectorLength = (long)BitConverter.ToInt32(new byte[] { ReturnByte[32], ReturnByte[33], ReturnByte[34], ReturnByte[35] }, 0);
}
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x52) //NTFS好象是64位
{
_SectorLength = BitConverter.ToInt64(new byte[] { ReturnByte[40], ReturnByte[41], ReturnByte[42], ReturnByte[43], ReturnByte[44], ReturnByte[45], ReturnByte[46], ReturnByte[47] }, 0);
}
}
///
/// 读一个扇区
///
/// 扇区号
/// 如果扇区数字大于分区信息的扇区数返回NULL
public byte[] ReadSector(long SectorIndex)
{
if (SectorIndex > _SectorLength) return null;
_DirverStream.Position = SectorIndex * 512;
byte[] ReturnByte = new byte[512];
_DirverStream.Read(ReturnByte, 0, 512); //获取扇区
return ReturnByte;
}
///
/// 写入数据
///
/// 扇区长度512
/// 扇区位置
public void WritSector(byte[] SectorBytes, long SectorIndex)
{
if (SectorBytes.Length != 512) return;
if (SectorIndex > _SectorLength) return;
_DirverStream.Position = SectorIndex * 512;
_DirverStream.Write(SectorBytes, 0, 512); //写入扇区
}
///
/// 关闭
///
public void Close()
{
_DirverStream.Close();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货