c#直接读写物理硬盘引导区

其实读写硬盘引导区和是不是c#没有关系,只要能用windows api的语言都可以。

原理就是用createfile这个api,获得物理硬盘的handle,然后用FileStream读取就可以了,FileStream有几个构造函数是以句柄方式初始化的。

\\.\PHYSICALDRIVE0  就是第一个物理硬盘了,其他的就是1、2、3……

[csharp]  view plain  copy
  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.InteropServices;  
  4.   
  5. namespace test  
  6. {  
  7.     class test  
  8.     {  
  9.         [DllImport("kernel32", SetLastError = true)]  
  10.         static extern IntPtr CreateFile(  
  11.         string FileName,   
  12.         FileAccess DesiredAccess,   
  13.         FileShare ShareMode,   
  14.         IntPtr SecurityAttributes,  
  15.         FileMode CreationDisposition,   
  16.         int FlagsAndAttributes,  
  17.         IntPtr hTemplate   
  18.         );  
  19.         public const int FILE_FLAG_NO_BUFFERING = 0x20000000;  
  20.         public static void Main()  
  21.         {  
  22.             IntPtr handle = CreateFile(@"\\.\PHYSICALDRIVE0", FileAccess.Read, FileShare.None, IntPtr.Zero, FileMode.Open, FILE_FLAG_NO_BUFFERING, IntPtr.Zero);  
  23.             FileStream disk = new FileStream(handle, FileAccess.Read);  
  24.             byte[] outBuffer = new byte[512];  
  25.             byte[] inBuffer = new byte[512];  
  26.             outBuffer[0] = 1;  
  27.             outBuffer[1] = 0;  
  28.             outBuffer[2] = 1;  
  29.             disk.Seek(0, SeekOrigin.Begin);  
  30.             for (int i = 0; i < 512; i++)  
  31.             {  
  32.                 inBuffer[i] = (byte)disk.ReadByte();  
  33.             }  
  34.             for (int i = 0; i < 512; i++)  
  35.             {  
  36.                 Console.Write(inBuffer[i].ToString("x2")+" ");  
  37.             }  
  38.             Console.Read();  
  39.         }  
  40.     }  
  41. }  

当然不只是可以读取引导区,整个硬盘都可以啊。

另外,既然获得了硬盘的FileStream了,那么就不止可以Read了,当然也可以Write了,不过FileAccess什么的就得改改了。

 

干脆把改写的也贴上吧,我在虚拟机里实验的,运行后就这样了,没敢在真机试,有兴趣的同学可以在真机运行一下以下代码。

[csharp]  view plain  copy
  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.InteropServices;  
  4.   
  5. namespace test  
  6. {  
  7.     class test  
  8.     {  
  9.         [DllImport("kernel32", SetLastError = true)]  
  10.         static extern IntPtr CreateFile(  
  11.         string FileName,   
  12.         uint DesiredAccess,   
  13.         uint ShareMode,   
  14.         IntPtr SecurityAttributes,  
  15.         uint CreationDisposition,   
  16.         int FlagsAndAttributes,  
  17.         IntPtr hTemplate   
  18.         );  
  19.         public const int FILE_FLAG_NO_BUFFERING = 0x20000000;  
  20.         public static void Main()  
  21.         {  
  22.             IntPtr handle = CreateFile(@"\\.\PHYSICALDRIVE0", 0x40000000|0x80000000, 0 , IntPtr.Zero, 3, FILE_FLAG_NO_BUFFERING, IntPtr.Zero);  
  23.             Console.WriteLine(handle.ToString());  
  24.             FileStream disk = new FileStream(handle,FileAccess.ReadWrite);  
  25.             byte[] bt = new byte[512];  
  26.             Random rnd = new Random();  
  27.             rnd.NextBytes(bt);  
  28.             for (int i = 0; i < bt.Length; i++)  
  29.             {  
  30.                 Console.Write(bt[i].ToString("x2") + " ");  
  31.             }  
  32.             disk.Seek(0, SeekOrigin.Begin);  
  33.             try  
  34.             {  
  35.                 disk.Write(bt, 0, 512);  
  36.             }  
  37.             catch (Exception e)  
  38.             {  
  39.                 Console.WriteLine(e.Message);  
  40.             }  
  41.         }  
  42.     }  
  43. }  

猜你喜欢

转载自blog.csdn.net/smart_ljh/article/details/51443962