c# 注册表操作,用于更新软件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;

namespace UpdateFile
{
    //注册表RegistryKey类在.NET的Microsoft.Win32命名空间中

    /// <summary>
    /// 用于更新管理软件文件使用
    /// 作者:WJS
    /// 时间:2013-12-24
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CopyFileToInstallLocation();
        }

        /// <summary>
        /// 复制文件到安装目录
        /// </summary>
        private void CopyFileToInstallLocation()
        {
            string InstallLocation = FindInstallationDirectory();
            if (InstallLocation==null)
            {
                labelControl.Text = "您未安装回弹仪数据处理软件!";
            } 
            else
            {//复制文件到安装目录
                //获取需要复制文件的路径
                string FileDir = System.IO.Directory.GetCurrentDirectory();
                FileDir += "\\Data\\";
                CopyFileToInstallDir(FileDir, InstallLocation);
                labelControl.Text = "更新完成!";
            }

        }

        /// <summary>
        /// 查找安装路径
        /// </summary>
        /// <returns></returns>
        private string FindInstallationDirectory()
        {//{F527CD43-4692-43FF-8716-172D4631F905}
            //InstallLocation 安装路径

            string MykeyName = "{F527CD43-4692-43FF-8716-172D4631F905}";
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", false);
            RegistryKey microsoft = software.OpenSubKey("Microsoft", false);
            RegistryKey windows = microsoft.OpenSubKey("Windows", false);
            RegistryKey currentVersion = windows.OpenSubKey("CurrentVersion", false);
            RegistryKey uninstall = currentVersion.OpenSubKey("Uninstall", false);

            string[] subkeyNames = uninstall.GetSubKeyNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == MykeyName)
                {
                    RegistryKey Name = uninstall.OpenSubKey(MykeyName, false);
                    string InstallLocation = (string)Name.GetValue("InstallLocation");
                    return InstallLocation;
                }
            }
            return null;
        }


        private void CopyFileToInstallDir( string FileDir,string InstallLocation)
        {
        
           //查找目标文件夹下所有文件和文件夹
           DirectoryInfo dires = new DirectoryInfo(FileDir);
           FileInfo[] files = dires.GetFiles();
           foreach (FileInfo f in files)
           {
               if (!Directory.Exists(InstallLocation))
                   Directory.CreateDirectory(InstallLocation);
               File.Copy(f.FullName, InstallLocation+f.Name,true);
           }
           DirectoryInfo[] dir = dires.GetDirectories();
           foreach (DirectoryInfo d in dir)
           {
               CopyFileToInstallDir(d.FullName,InstallLocation+d.Name+"\\");
           }

        }



















    }
}

去年年末才看过一本关于ASP.NET 操作注册表的书,可惜那时候就压根没想过写到网上去。 现在想想 写到网上有诸多好处 比如、 1、可以快速回忆相关知识点,还有自己写的参考代码,比别人写的强多了 2、可以丰富自己的博客推广自己 3、加强自己写作能力 贴出代码了 -------------------- 1、引入命名空间 using Microsoft.Win32;//修改注册表所需要用到的命名空间 2、之后方法喽 //修改注册表 设置IE打印背景图片 适用于IE8一下版本,修改为工具选项下的 打印背景颜色与图像 public void IESetupPrint_Background() { RegistryKey key = Registry.CurrentUser; RegistryKey software = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main", true); //该项必须已存在 software.SetValue("Print_Background", "yes"); software.Close(); } //设置页眉 页脚为空 public void IESetupPageSetup() { RegistryKey key = Registry.CurrentUser; RegistryKey software = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true); //该项必须已存在 software.SetValue("header", ""); software.SetValue("footer", ""); software.Close(); } //设置页边距 设置IE页面设置 打印背景图片 //上下边距具体的值可以 先在IE里面设置再查看注册表实际值 然后在程序里写死 public void IESetupPage() { RegistryKey key = Registry.CurrentUser;//IE8以上版本 修改为页面设置 里页面 打印背景颜色与图像 RegistryKey software = key.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true); software.SetValue("margin_top", 0.55984); software.SetValue("margin_bottom", 0.55984); software.SetValue("margin_left", 0.75433); software.SetValue("margin_right", 0.40000); software.SetValue("Print_Background", "yes"); } //查看注册表相应的键值 运行输入 regedit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值