自定义安装之操作注册表

public class SetupHelper
    {
        /// <summary>
        /// 创建程序快捷键
        /// </summary>
        /// <param name="targetpath">程序运行地址</param>
        /// <param name="shortcutname">快捷键名称</param>
        /// <param name="description">说明</param>
        /// <param name="iconlocation">快捷键图标</param>
        public void CreateShortcutOnDesktop(string targetpath, string shortcutname, string description, string iconlocation)
        {
            //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary;            
            String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), shortcutname + ".lnk");
            if (!System.IO.File.Exists(shortcutPath))
            {
                IWshShell shell = new WshShell();
                // 确定是否已经创建的快捷键被改名了                
                foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
                {
                    WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item);
                    if (tempShortcut.TargetPath == targetpath)
                    {
                        return;
                    }
                }
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
                shortcut.TargetPath = targetpath;//指定目标路径
                shortcut.WorkingDirectory = Path.GetDirectoryName(targetpath);//设置起始位置
                shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
                shortcut.Description = description;//设置备注
                shortcut.IconLocation = string.IsNullOrWhiteSpace(iconlocation) ? targetpath : iconlocation;//设置图标路径
                shortcut.Save();//保存快捷方式
            }
        }

        /// <summary>
        /// 创建程序菜单快捷方式
        /// </summary>
        /// <param name="targetPath">可执行文件路径</param>
        /// <param name="description">快捷方式名称</param>
        /// <param name="menuName">程序菜单中子菜单名称,为空则不创建子菜单</param>
        /// <param name="iconLocation">快捷方式图标路径</param>
        /// <param name="workingDirectory">工作路径</param>
        /// <returns></returns>
        public static void CreateProgramsShortcut(string targetpath, string shortcutname, string description, string iconlocation)
        {
            String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), shortcutname + ".lnk");
            if (!System.IO.File.Exists(shortcutPath))
            {
                IWshShell shell = new WshShell();
                // 确定是否已经创建的快捷键被改名了                
                foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
                {
                    WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item);
                    if (tempShortcut.TargetPath == targetpath)
                    {
                        return;
                    }
                }
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
                shortcut.TargetPath = targetpath;//指定目标路径
                shortcut.WorkingDirectory = Path.GetDirectoryName(targetpath);//设置起始位置
                shortcut.WindowStyle = 1;//设置运行方式,默认为常规窗口
                shortcut.Description = description;//设置备注
                shortcut.IconLocation = string.IsNullOrWhiteSpace(iconlocation) ? targetpath : iconlocation;//设置图标路径
                shortcut.Save();//保存快捷方式
            }
        }


        /// <summary>  
        /// 修改程序在注册表中的键值  开机自启
        /// 注册表地址:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
        /// </summary>  
        /// <param name="isauto">true:开机启动,false:不开机自启</param> 
        public static void AutoStart(string softname, bool isauto, string path)
        {
            try
            {
                if (isauto == true)
                {
                    RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.SetValue(softname, path);
                    R_run.Close();
                    R_local.Close();
                }
                else
                {
                    RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue(softname, false);
                    R_run.Close();
                    R_local.Close();
                }
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 将程序添加到控制面板的卸载内
        /// </summary>
        /// <param name="softname"></param>
        /// <param name="softwareinfo"></param>
        public static void Uninstall(string softname, SoftwareInfo softwareinfo)
        {
            RegistryKey soft = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" + softname);
            soft.SetValue("DisplayIcon", softwareinfo.DisplayIcon);
            soft.SetValue("DisplayName", softwareinfo.DisplayName);
            soft.SetValue("DisplayVersion", softwareinfo.DisplayVersion);
            soft.SetValue("EstimatedSize", softwareinfo.EstimatedSize);//大小
            soft.SetValue("URLInfoAbout", softwareinfo.URLInfoAbout);//支持链接
            soft.SetValue("InstallLocation", softwareinfo.InstallLocation);//安装目录
            soft.SetValue("Publisher", softwareinfo.Publisher);//发布者
            soft.SetValue("UninstallString", softwareinfo.UninstallString);//卸载程序路径
            soft.Close();
        }
    }
public class SoftwareInfo
    {
        /// <summary>
        /// 程序图标
        /// </summary>
        public string DisplayIcon { get; set; }
        /// <summary>
        /// 程序名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 程序版本
        /// </summary>
        public string DisplayVersion { get; set; }
        /// <summary>
        /// 程序大小
        /// </summary>
        public long EstimatedSize { get; set; }
        /// <summary>
        /// 帮助链接
        /// </summary>
        public string URLInfoAbout { get; set; }
        /// <summary>
        /// 安装目录
        /// </summary>
        public string InstallLocation { get; set; }
        /// <summary>
        /// 发布者
        /// </summary>
        public string Publisher { get; set; }
        /// <summary>
        /// 卸载程序路径
        /// </summary>
        public string UninstallString { get; set; }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值