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;
}
}