隐藏磁盘驱动器

Posted on 2019-01-29 13:15  努力成长静待花开  阅读(190)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:
  

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] bytes;
            string flags="";
            if (radioButton1.Checked == true)
            {
                bytes = new byte[] { 04, 00, 00, 00 };
                flags = "C盘";
            }
            else if(radioButton2.Checked==true)
            {
                bytes = new byte[] { 08,00,00,00 };
                flags = "D盘";
            }
            else if (radioButton3.Checked == true)
            {
                bytes = new byte[] { 16, 00, 00, 00 };
                flags = "E盘";
            }
            else
            { return; }
            HideDisk(bytes);
            if (MessageBox.Show("隐藏磁盘:" + flags + "成功") == DialogResult.OK)
                RefreshSystem();
        }
        private void HideDisk(byte[] bts)
        {
            try
            {
                RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer");
                rk.SetValue("NoDrives",bts,RegistryValueKind.Binary);
            }
            catch (Exception E)
            { MessageBox.Show(E.Message); }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            HideDisk(new byte[] { 16, 16, 16, 16, 16, 16, 16, 16});
            RefreshSystem();
        }