注册表注册文件以及其关联程序

using Microsoft.Win32;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace RegisterTool

{

    public class Register

    {

        [Flags]

        public enum HChangeNotifyFlags

        {

            SHCNF_DWORD = 0x0003,

            SHCNF_IDLIST = 0x0000,

            SHCNF_PATHA = 0x0001,

            SHCNF_PATHW = 0x0005,

            SHCNF_PRINTERA = 0x0002,

            SHCNF_PRINTERW = 0x0006,

            SHCNF_FLUSH = 0x1000,

            SHCNF_FLUSHNOWAIT = 0x2000

        }

        [Flags]

        enum HChangeNotifyEventID

        {

            SHCNE_ALLEVENTS = 0x7FFFFFFF,

            SHCNE_ASSOCCHANGED = 0x08000000,

            SHCNE_ATTRIBUTES = 0x00000800,

            SHCNE_CREATE = 0x00000002,

            SHCNE_DELETE = 0x00000004,

            SHCNE_DRIVEADD = 0x00000100,

            SHCNE_DRIVEADDGUI = 0x00010000,

            SHCNE_DRIVEREMOVED = 0x00000080,

            SHCNE_EXTENDED_EVENT = 0x04000000,

            SHCNE_FREESPACE = 0x00040000,

            SHCNE_MEDIAINSERTED = 0x00000020,

            SHCNE_MEDIAREMOVED = 0x00000040,

            SHCNE_MKDIR = 0x00000008,

            SHCNE_NETSHARE = 0x00000200,

            SHCNE_NETUNSHARE = 0x00000400,

            SHCNE_RENAMEFOLDER = 0x00020000,

            SHCNE_RENAMEITEM = 0x00000001,

            SHCNE_RMDIR = 0x00000010,

            SHCNE_SERVERDISCONNECT = 0x00004000,

            SHCNE_UPDATEDIR = 0x00001000,

            SHCNE_UPDATEIMAGE = 0x00008000,

        }

        [DllImport("shell32.dll")]

        static extern void SHChangeNotify(HChangeNotifyEventID wEventId,

                                          HChangeNotifyFlags uFlags,

                                          IntPtr dwItem1,

                                          IntPtr dwItem2);

        public void Registered(string ExecutablePath, string ico)

        {

            try

            {

                UnRegistered(ExecutablePath, ico, ".MTask");

            }

            catch (Exception ex)

            {

                //

            }

            try

            {

                string appName = ExecutablePath;// Application.ExecutablePath;

                RegistryKey hk_root = Registry.ClassesRoot; //HKEY_CLASSES_ROOT

                //注册自己程序的the ProgID     

                if (null != hk_root.OpenSubKey(ExType)) //The proper format of a ProgID key name is [Vendor or Application].[Component].[Version], separated by periods and with no spaces, as in Word.Document.6. The Version portion is optional but strongly recommended (see Using Versioned PROGIDs).

                {

                    hk_root.DeleteSubKeyTree(ExType);

                }

                if (null == hk_root.OpenSubKey(ExType)) //The proper format of a ProgID key name is [Vendor or Application].[Component].[Version], separated by periods and with no spaces, as in Word.Document.6. The Version portion is optional but strongly recommended (see Using Versioned PROGIDs).

                {

                    RegistryKey progID = hk_root.CreateSubKey(ExType);

                    if (progID == null)

                    {

                        MessageBox.Show(String.Format("Create{0} progID faild!", ExType), "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        hk_root.Close();

                        return;

                    }

                    //创建(默认)键值,显示给用户看的文件类型描述             

                    progID.SetValue("", "XBus的task文件 ", RegistryValueKind.String);

                    //文件显示的图标

                    RegistryKey defaultIcon = progID.CreateSubKey("DefaultIcon");

                    if (defaultIcon == null)

                    {

                        MessageBox.Show(String.Format("Create{0} defaultIcon faild!", ExType), "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        progID.Close();

                        hk_root.Close();

                        return;

                    }

                    //创建(默认)键值,指定文件显示图标           

                    defaultIcon.SetValue("", ico + ",0");

                    defaultIcon.Close();

                    //指定文件动作

                    RegistryKey shell = progID.CreateSubKey("shell");

                    if (shell == null)

                    {

                        MessageBox.Show(String.Format("Create{0} shell failed!", ExType), "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        progID.Close();

                        hk_root.Close();

                        return;

                    }

                    //open 动作------------------------

                    RegistryKey open = shell.CreateSubKey("open");

                    if (open == null)

                    {

                        MessageBox.Show(String.Format("Create{0} open failed!", ExType), "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        shell.Close();

                        progID.Close();

                        hk_root.Close();

                        return;

                    }

                    RegistryKey command = open.CreateSubKey("command");

                    if (command == null)

                    {

                        MessageBox.Show(String.Format("Create {0} command failed!", ExType), "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        open.Close();

                        shell.Close();

                        progID.Close();

                        hk_root.Close();

                        return;

                    }

                    command.SetValue("", String.Format("\"{0}\" \"%1\"", appName), RegistryValueKind.String);

                    command.Close();

                    open.Close();

                    progID.Close();

                }

                //修改对应文件类型的默认的关联程序

                //因为在系统里面一把mp3都是已经注册的了,所以这里只是简单修改一下关联

                RegistryKey dat = hk_root.OpenSubKey(ExType, true);

                if (dat != null)

                {

                    dat.SetValue("", ExType, RegistryValueKind.String); //指定用我们 上面的ProgID打开mp3文件

                    dat.Close();

                    MessageBox.Show(" Register Success !");

                }

                else

                {

                    MessageBox.Show(" Register failed !", "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

                hk_root.Close();

                //通知系统,文件关联已经是作用,不然可能要等到系统重启才看到效果

                SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);

            }

            catch (Exception ex)

            {

                MessageBox.Show(" Registered  failed !", "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }

        }

        public void UnRegistered(string ExecutablePath, string ico, string ExType)

        {

            try

            {

                string appName = ExecutablePath;// Application.ExecutablePath;

                RegistryKey hk_root = Registry.ClassesRoot; //HKEY_CLASSES_ROOT

                //注册自己程序的the ProgID     

                if (null != hk_root.OpenSubKey(ExType)) //The proper format of a ProgID key name is [Vendor or Application].[Component].[Version], separated by periods and with no spaces, as in Word.Document.6. The Version portion is optional but strongly recommended (see Using Versioned PROGIDs).

                {

                    hk_root.DeleteSubKeyTree(ExType);

                }

                //MessageBox.Show(" UnRegister Success !");

                hk_root.Close();

                //通知系统,文件关联已经是作用,不然可能要等到系统重启才看到效果

                SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "System Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }

        }

        private const string ExType = XBusExtensionName.GTskExtension;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 3,229评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,715评论 19 139
  • #坚持写作30天# 2017-07-11(192/365) Day11 每当夜深人静或者等到同事们都离开去睡觉的...
    开拓者2021阅读 611评论 0 0
  • 论文写太久几近窒息 学校本该是青春洋溢的地方 却处处感受到靡靡之气 出去走走吧 换衣服背背包 去吸收普罗大众的气息...
    猴子不上树阅读 237评论 0 0
  • 模仿与被模仿 天才与技艺(规律)
    鑫丫头xlx阅读 519评论 5 7

友情链接更多精彩内容