通过注册表设置桌面背景

Posted on 2019-01-23 10:14  努力成长静待花开  阅读(727)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  RegistryKey类的OpenSubKey方法  //用来检索指定的子项

    public RegistryKey OpenSubKey (string name,bool writable)  //writable:如果需要项的写访问权限 设为true

实现代码:

        private void button2_Click(object sender, EventArgs e)
        {
            string path = textBox1.Text;                 
            RegistryKey rkey = Registry.CurrentUser;    //获取基表
            rkey = rkey.OpenSubKey(@"Control Panel\Desktop",true);  //检索指定子项
            rkey.SetValue("WallPaper",path);
            rkey.SetValue("TileWallpaper","2");
            rkey.Close();       //关闭注册表
            MessageBox.Show("设置成功 请重新启动","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }