Exemplo n.º 1
0
        public ContextMenuKey(RegistryKey key)
        {
            if (key == null)
            {
                throw new Exception("KEY IS NULL");
            }

            if (false)
            {
                throw new Exception("This is not a context menu key");
            }

            this.FullNameRegistryKey = key.Name;

            //получение имени ключа
            for (int i = this.FullNameRegistryKey.Length - 1; i > 0; i--)
            {
                if (this.FullNameRegistryKey[i] == '\\')
                {
                    this.registryName = this.FullNameRegistryKey.Substring(i + 1, this.FullNameRegistryKey.Length - i - 1);
                    break;
                }
            }

            this.title = this.registryName;

            //проверка на существование подраздела
            string[] subsection = key.GetSubKeyNames();
            if (subsection.Length != 0)
            {
                if (subsection.Length > 1)
                {
                    throw new Exception("Найдено больше одного подраздела у ключа: " + key.Name);
                }

                if (subsection[0] == "command")
                {
                    //чтение подраздела 'command'
                    RegistryKey command = key.OpenSubKey("command");
                    foreach (string keyParam in command.GetValueNames())
                    {
                        if (keyParam == "")
                        {
                            this.command = (string)command.GetValue(""); break;
                        }
                        if (keyParam == "DelegateExecute")
                        {
                            this.command = (string)command.GetValue("DelegateExecute"); break;
                        }
                    }
                }
                else if (subsection[0] == "shell" || subsection[0] == "Shell" || subsection[0] == "DropTarget")
                {
                }
                else
                {
                    throw new Exception("Неизвестный подраздел");
                }
            }

            //получение параметров ключа и работа с известными
            string[] parameters = key.GetValueNames();
            for (int i = 0; i < parameters.Length; i++)
            {
                KeyParam param = new KeyParam()
                {
                    Name  = parameters[i],
                    Type  = key.GetValueKind(parameters[i]),
                    Value = key.GetValue(parameters[i], null, RegistryValueOptions.None)
                };

                switch (param.Name)
                {
                case "": /*this.command = (string)param.Value;*/ break;

                case "SubCommands":

                    if ((string)param.Value == string.Empty)
                    {
                        this.isMenu = true;
                    }
                    else
                    {
                        this.isMenu = null;
                    }

                    break;

                case "ExtendedSubCommandsKey": this.isMenu = true; break;

                case "MUIVerb":
                    this.title   = (string)param.Value;
                    this.muiVerb = (string)param.Value; break;

                case "Position":
                    switch (((string)param.Value).ToLower())
                    {
                    case "top": this.position = true; break;

                    case "bottom": this.position = false; break;

                    default: this.position = null; break;
                    }
                    break;

                case "Extended": this.extended = true; break;
                }
            }

            //дополнительный блок анализа имени ключа : в идеале его не должно быть, но я пока не умею выполнять длл !
            Regex regex = new Regex(@"\.(dll|exe)");
            Match rez   = regex.Match(this.title);

            if (rez.Value != string.Empty)
            {
                this.title = this.registryName + "_(dll)";
            }

            //дополнительный блок анализа команды ключа : в идеале его не должно быть, но я пока не умею выполнять CLSID !
            if (this.command != null)
            {
                Regex regex2 = new Regex(@"{[^\s^-]{8}-[^\s^-]{4}-[^\s^-]{4}-[^\s^-]{4}-[^\s^-]{12}}");
                Match rez2   = regex2.Match(this.Command);
                if (rez2.Value != string.Empty)
                {
                    this.command  = "this command is a CLSID";
                    this.disabled = true;
                }
            }
        }
Exemplo n.º 2
0
        private ContextMenuKey Deserialize(string str)
        {
            RegistryKey TargetKey = null;

            Regex           regexKey           = new Regex("\\[(.*)\\]");
            MatchCollection regexKeyCollection = regexKey.Matches(str);

            if (regexKeyCollection.Count > 0)
            {
                //пробег по ключам
                for (int i = 0; i < regexKeyCollection.Count; i++)
                {
                    GroupCollection groupKey     = regexKeyCollection[i].Groups;
                    string          pathRegexKey = groupKey[1].Value;

                    //востановление ключа
                    RegistryKey registryKey = null;
                    {
                        string[] path = pathRegexKey.Split(new char[] { '\\' });

                        switch (path[0])
                        {
                        case "HKEY_CLASSES_ROOT": registryKey = Microsoft.Win32.Registry.ClassesRoot; break;

                        case "HKEY_CURRENT_USER": registryKey = Microsoft.Win32.Registry.CurrentUser; break;

                        case "HKEY_LOCAL_MACHINE": registryKey = Microsoft.Win32.Registry.LocalMachine; break;

                        case "HKEY_USERS": registryKey = Microsoft.Win32.Registry.Users; break;

                        case "HKEY_CURRENT_CONFIG": registryKey = Microsoft.Win32.Registry.CurrentConfig; break;
                        }

                        for (int iii = 1; iii < path.Length - 1; iii++)
                        {
                            registryKey = registryKey.OpenSubKey(path[iii], true);
                        }

                        registryKey = registryKey.CreateSubKey(path[path.Length - 1]);
                    }
                    if (i == 0)
                    {
                        TargetKey = registryKey;
                    }



                    int startindex = groupKey[0].Index;
                    int lastindex;
                    if (i == regexKeyCollection.Count - 1)
                    {
                        lastindex = str.Length;
                    }
                    else
                    {
                        lastindex = regexKeyCollection[i + 1].Groups[0].Index;
                    }

                    //пробег по параметрам
                    Regex           regexKeyParam           = new Regex("(?:(?:\"(.*)\"|(@))=\"(.*)\")");
                    MatchCollection regexKeyParamCollection = regexKeyParam.Matches(str.Substring(startindex, lastindex - startindex));
                    if (regexKeyParamCollection.Count > 0)
                    {
                        for (int ii = 0; ii < regexKeyParamCollection.Count; ii++)
                        {
                            GroupCollection groupKeyParam = regexKeyParamCollection[ii].Groups;

                            KeyParam param = new KeyParam
                            {
                                Name  = groupKeyParam[2].Value == "@" ? "" : groupKeyParam[1].Value,
                                Value = groupKeyParam[3].Value
                            };

                            registryKey.SetValue(param.Name, param.Value);
                        }
                    }
                }
            }
            else
            {
                throw new Exception("EmptyFile");
            }

            if (TargetKey == null)
            {
                throw new Exception("TargetKey is null.");
            }

            return(new ContextMenuKey(TargetKey));
        }