Exemple #1
0
 protected override void InitializeComponents()
 {
     base.InitializeComponents();
     this.Text        = AppString.Dialog.NewOpenWithItem;
     btnBrowse.Click += (sender, e) => BrowseFile();
     btnOk.Click     += (sender, e) =>
     {
         if (string.IsNullOrEmpty(ItemText))
         {
             MessageBoxEx.Show(AppString.MessageBox.TextCannotBeEmpty);
             return;
         }
         if (string.IsNullOrWhiteSpace(ItemCommand))
         {
             MessageBoxEx.Show(AppString.MessageBox.TextCannotBeEmpty);
             return;
         }
         FilePath   = ObjectPath.ExtractFilePath(ItemCommand);
         AppRegPath = $@"HKEY_CLASSES_ROOT\Applications\{Path.GetFileName(FilePath)}";
         if (FilePath == null || RegistryEx.GetRegistryKey(AppRegPath) != null)
         {
             MessageBoxEx.Show(AppString.MessageBox.UnsupportedFilename);
             return;
         }
         AddNewItem();
         this.DialogResult = DialogResult.OK;
     };
 }
Exemple #2
0
        public static string GetFilePath(Guid guid)
        {
            string filePath = null;

            if (guid.Equals(Guid.Empty))
            {
                return(filePath);
            }
            if (FilePathDic.ContainsKey(guid))
            {
                filePath = FilePathDic[guid];
            }
            else
            {
                foreach (string clsidPath in ClsidPaths)
                {
                    using (RegistryKey guidKey = RegistryEx.GetRegistryKey($@"{clsidPath}\{guid:B}"))
                    {
                        if (guidKey == null)
                        {
                            continue;
                        }
                        foreach (string keyName in new[] { "InprocServer32", "LocalServer32" })
                        {
                            using (RegistryKey key = guidKey.OpenSubKey(keyName))
                            {
                                if (key == null)
                                {
                                    continue;
                                }
                                string value1 = key.GetValue("CodeBase")?.ToString()?.Replace("file:///", "")?.Replace('/', '\\');
                                if (File.Exists(value1))
                                {
                                    filePath = value1; break;
                                }
                                string value2 = key.GetValue("")?.ToString();
                                value2 = ObjectPath.ExtractFilePath(value2);
                                if (File.Exists(value2))
                                {
                                    filePath = value2; break;
                                }
                            }
                        }
                        if (File.Exists(filePath))
                        {
                            break;
                        }
                    }
                }
                FilePathDic.Add(guid, filePath);
            }
            return(filePath);
        }
        private void LoadOpenWithItems()
        {
            using (RegistryKey root = Registry.ClassesRoot)
                using (RegistryKey appKey = root.OpenSubKey("Applications"))
                {
                    foreach (string appName in appKey.GetSubKeyNames())
                    {
                        if (!appName.Contains('.'))
                        {
                            continue;                   //需要为有扩展名的文件名
                        }
                        using (RegistryKey shellKey = appKey.OpenSubKey($@"{appName}\shell"))
                        {
                            if (shellKey == null)
                            {
                                continue;
                            }

                            List <string> names = shellKey.GetSubKeyNames().ToList();
                            if (names.Contains("open", StringComparer.OrdinalIgnoreCase))
                            {
                                names.Insert(0, "open");
                            }

                            string keyName = names.Find(name =>
                            {
                                using (RegistryKey cmdKey = shellKey.OpenSubKey(name))
                                    return(cmdKey.GetValue("NeverDefault") == null);
                            });
                            if (keyName == null)
                            {
                                continue;
                            }

                            using (RegistryKey commandKey = shellKey.OpenSubKey($@"{keyName}\command"))
                            {
                                string command = commandKey?.GetValue("")?.ToString();
                                if (ObjectPath.ExtractFilePath(command) != null)
                                {
                                    this.AddItem(new OpenWithItem(commandKey.Name));
                                }
                            }
                        }
                    }
                }
        }
Exemple #4
0
            protected override void InitializeComponents()
            {
                base.InitializeComponents();
                this.Text = AppString.Dialog.NewSendToItem;
                this.Controls.AddRange(new[] { rdoFile, rdoFolder });
                rdoFile.Top    = rdoFolder.Top = btnOk.Top;
                rdoFile.Left   = lblCommand.Left;
                rdoFolder.Left = rdoFile.Right + 20.DpiZoom();

                btnBrowse.Click += (sender, e) =>
                {
                    if (rdoFile.Checked)
                    {
                        BrowseFile();
                    }
                    else
                    {
                        BrowseFolder();
                    }
                };

                btnOk.Click += (sender, e) =>
                {
                    if (ItemText.IsNullOrWhiteSpace())
                    {
                        MessageBoxEx.Show(AppString.MessageBox.TextCannotBeEmpty);
                        return;
                    }
                    if (ItemCommand.IsNullOrWhiteSpace())
                    {
                        MessageBoxEx.Show(AppString.MessageBox.CommandCannotBeEmpty);
                        return;
                    }
                    if (ObjectPath.ExtractFilePath(ItemCommand) == null && !Directory.Exists(ItemCommand))
                    {
                        MessageBoxEx.Show(AppString.MessageBox.FileOrFolderNotExists);
                        return;
                    }
                    AddNewItem();
                    DialogResult = DialogResult.OK;
                };
            }
            private void AddNewItem()
            {
                FilePath = $@"{SendToList.SendToPath}\{ObjectPath.RemoveIllegalChars(ItemText)}.lnk";
                FilePath = ObjectPath.GetNewPathWithIndex(FilePath, ObjectPath.PathType.File);

                IWshRuntimeLibrary.IWshShortcut shortcut = WshShell.CreateShortcut(FilePath);
                if (rdoFile.Checked)
                {
                    ItemCommand         = Environment.ExpandEnvironmentVariables(ItemCommand);
                    shortcut.TargetPath = ObjectPath.ExtractFilePath(ItemCommand, out string shortPath);
                    string str = ItemCommand.Substring(ItemCommand.IndexOf(shortPath) + shortPath.Length);
                    shortcut.Arguments        = str.Substring(str.IndexOf(" ") + 1);
                    shortcut.WorkingDirectory = Path.GetDirectoryName(shortcut.TargetPath);
                }
                else
                {
                    shortcut.TargetPath = ItemCommand;
                }
                shortcut.Save();
                DesktopIniHelper.SetLocalizedFileName(FilePath, ItemText);
            }
Exemple #6
0
 protected override void InitializeComponents()
 {
     base.InitializeComponents();
     btnBrowse.Click += (sender, e) => BrowseFile();
     btnOk.Click     += (sender, e) =>
     {
         if (string.IsNullOrEmpty(ItemText))
         {
             MessageBoxEx.Show(AppString.Message.TextCannotBeEmpty);
             return;
         }
         if (ItemCommand.IsNullOrWhiteSpace())
         {
             MessageBoxEx.Show(AppString.Message.CommandCannotBeEmpty);
             return;
         }
         FilePath = ObjectPath.ExtractFilePath(base.ItemFilePath);
         using (var key = RegistryEx.GetRegistryKey(CommandPath))
         {
             string path = ObjectPath.ExtractFilePath(key?.GetValue("")?.ToString());
             string name = Path.GetFileName(path);
             if (FilePath != null && FilePath.Equals(path, StringComparison.OrdinalIgnoreCase))
             {
                 MessageBoxEx.Show(AppString.Message.HasBeenAdded);
                 return;
             }
             if (FileName == null || FileName.Equals(name, StringComparison.OrdinalIgnoreCase))
             {
                 MessageBoxEx.Show(AppString.Message.UnsupportedFilename);
                 return;
             }
         }
         AddNewItem();
         this.DialogResult = DialogResult.OK;
     };
 }
 private void LoadShellItems(XmlElement shellXE, GroupPathItem groupItem)
 {
     foreach (XmlElement itemXE in shellXE.SelectNodes("Item"))
     {
         if (!JudgeOSVersion(itemXE))
         {
             continue;
         }
         if (!FileExists(itemXE))
         {
             continue;
         }
         XmlElement szXE    = (XmlElement)itemXE.SelectSingleNode("Value/REG_SZ");
         string     keyName = itemXE.GetAttribute("KeyName");
         if (keyName.IsNullOrWhiteSpace())
         {
             continue;
         }
         EnhanceShellItem item = new EnhanceShellItem()
         {
             RegPath       = $@"{groupItem.TargetPath}\shell\{keyName}",
             FoldGroupItem = groupItem,
             ItemXE        = itemXE
         };
         if (szXE != null)
         {
             item.Text = ResourceString.GetDirectString(szXE.GetAttribute("MUIVerb"));
             if (szXE.HasAttribute("Icon"))
             {
                 item.Image = ResourceIcon.GetIcon(szXE.GetAttribute("Icon"))?.ToBitmap();
             }
             else if (szXE.HasAttribute("HasLUAShield"))
             {
                 item.Image = AppImage.Shield;
             }
             else
             {
                 XmlElement cmdXE = (XmlElement)itemXE.SelectSingleNode("SubKey/Command");
                 if (cmdXE != null)
                 {
                     Icon icon = null;
                     if (cmdXE.HasAttribute("Default"))
                     {
                         string filePath = ObjectPath.ExtractFilePath(cmdXE.GetAttribute("Default"));
                         icon = ResourceIcon.GetIcon(filePath);
                     }
                     item.Image = icon?.ToBitmap();
                     icon?.Dispose();
                 }
             }
         }
         if (item.Image == null)
         {
             item.Image = AppImage.NotFound;
         }
         if (item.Text.IsNullOrWhiteSpace())
         {
             item.Text = keyName;
         }
         item.ChkVisible.Checked = item.ItemVisible;
         string tip = itemXE.GetAttribute("Tip");
         if (itemXE.GetElementsByTagName("CreateFile").Count > 0)
         {
             if (!tip.IsNullOrWhiteSpace())
             {
                 tip += "\n";
             }
             tip += AppString.Tip.CommandFiles;
             if (System.Diagnostics.Debugger.IsAttached)
             {
                 item.ChkVisible.Checked = item.ItemVisible = true;
             }
         }
         MyToolTip.SetToolTip(item.ChkVisible, tip);
         this.AddItem(item);
     }
 }
 private void LoadShellItems(XmlNode shellXN, FoldGroupItem groupItem)
 {
     foreach (XmlElement itemXE in shellXN.SelectNodes("Item"))
     {
         if (!XmlDicHelper.FileExists(itemXE))
         {
             continue;
         }
         if (!XmlDicHelper.JudgeCulture(itemXE))
         {
             continue;
         }
         if (!XmlDicHelper.JudgeOSVersion(itemXE))
         {
             continue;
         }
         string keyName = itemXE.GetAttribute("KeyName");
         if (keyName.IsNullOrWhiteSpace())
         {
             continue;
         }
         EnhanceShellItem item = new EnhanceShellItem()
         {
             RegPath       = $@"{groupItem.GroupPath}\shell\{keyName}",
             FoldGroupItem = groupItem,
             ItemXE        = itemXE
         };
         foreach (XmlElement szXE in itemXE.SelectNodes("Value/REG_SZ"))
         {
             if (!XmlDicHelper.JudgeCulture(szXE))
             {
                 continue;
             }
             if (szXE.HasAttribute("MUIVerb"))
             {
                 item.Text = ResourceString.GetDirectString(szXE.GetAttribute("MUIVerb"));
             }
             if (szXE.HasAttribute("Icon"))
             {
                 item.Image = ResourceIcon.GetIcon(szXE.GetAttribute("Icon"))?.ToBitmap();
             }
             else if (szXE.HasAttribute("HasLUAShield"))
             {
                 item.Image = AppImage.Shield;
             }
         }
         if (item.Image == null)
         {
             XmlElement cmdXE = (XmlElement)itemXE.SelectSingleNode("SubKey/Command");
             if (cmdXE != null)
             {
                 Icon icon = null;
                 if (cmdXE.HasAttribute("Default"))
                 {
                     string filePath = ObjectPath.ExtractFilePath(cmdXE.GetAttribute("Default"));
                     icon = ResourceIcon.GetIcon(filePath);
                 }
                 else
                 {
                     XmlNode fileXE = cmdXE.SelectSingleNode("FileName");
                     if (fileXE != null)
                     {
                         string filePath = ObjectPath.ExtractFilePath(fileXE.InnerText);
                         icon = ResourceIcon.GetIcon(filePath);
                     }
                 }
                 item.Image = icon?.ToBitmap();
                 icon?.Dispose();
             }
         }
         if (item.Image == null)
         {
             item.Image = AppImage.NotFound;
         }
         if (item.Text.IsNullOrWhiteSpace())
         {
             item.Text = keyName;
         }
         string tip = "";
         foreach (XmlElement tipXE in itemXE.SelectNodes("Tip"))
         {
             if (XmlDicHelper.JudgeCulture(tipXE))
             {
                 tip = tipXE.GetAttribute("Value");
             }
         }
         if (itemXE.GetElementsByTagName("CreateFile").Count > 0)
         {
             if (!tip.IsNullOrWhiteSpace())
             {
                 tip += "\n";
             }
             tip += AppString.Tip.CommandFiles;
         }
         ToolTipBox.SetToolTip(item.ChkVisible, tip);
         this.AddItem(item);
     }
 }