最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Integration in Windows Shell context menu for multiple files with selection dispaly of menu - Stack Overflow

programmeradmin0浏览0评论
  1. Add Windows context menu as "Merge PDF" when selecting multiple PDF files. After the addition, display only if multiple PDF files are selected. When the user clicks on the "Merge PDF", starts as a single instance of the application and loads all selected pdf on the listview.

  2. Add Windows context menu as "Open PDF" when selecting single PDF files. After the addition, display only if single PDF files are selected. When the user clicks on the "Merge PDF", starts as a single instance of the application and displays a file name in the message box.

Actually, the I wrote original code in vb. As we know C# is used more widely than vb for that reason, I just made a conversion into the C# for easy understanding for C# developers.

I trying and add it to the Windows context menu.

private void cmdRegisterContextMenu_Click(object sender, EventArgs e)
{
    // Registry path for PDF files
    string pdfKeyPath = @"SystemFileAssociations\.pdf\Shell";
    string openKeyPath = pdfKeyPath + @"\Open PDF";
    string mergeKeyPath = pdfKeyPath + @"\Merge PDF";

    // Path to your application executable
    string appPath = "\"" + Application.ExecutablePath + "\"";

    // Register "Open PDF" for single file selection
    using (RegistryKey openKey = Registry.ClassesRoot.CreateSubKey(openKeyPath))
    {
        openKey.SetValue("", "Open PDF");
        openKey.SetValue("Icon", appPath);
    }

    using (RegistryKey openCommandKey = Registry.ClassesRoot.CreateSubKey(openKeyPath + @"\Command"))
    {
        openCommandKey.SetValue("", appPath + " \"%1\"");
    }

    // Register "Merge PDF" for multiple file selection
    using (RegistryKey mergeKey = Registry.ClassesRoot.CreateSubKey(mergeKeyPath))
    {
        mergeKey.SetValue("", "Merge PDF");
        mergeKey.SetValue("App", appPath);
        mergeKey.SetValue("Icon", @"E:\My Software\WindowsApp29\Project\Extra\graph_chart.ico");
    }

    using (RegistryKey mergeCommandKey = Registry.ClassesRoot.CreateSubKey(mergeKeyPath + @"\Command"))
    {
        mergeCommandKey.SetValue("", appPath + " \"%*\"");
    }

    MessageBox.Show("Context menu entries registered successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void ContextMenu_Load(object sender, EventArgs e)
    {
        string[] args = Environment.GetCommandLineArgs();

        if (args.Length > 1)
        {
            var pdfFiles = new List<string>();

            for (int i = 1, loopTo = args.Length - 1; i <= loopTo; i++)
                pdfFiles.Add(args[i]);

            LoadPDFsIntoListView(pdfFiles);
        }

    }


public void LoadPDFsIntoListView(List<string> pdfFiles)
{
    foreach (string pdfFile in pdfFiles)
    {
        var item = new ListViewItem(pdfFile);
        ListView1.Items.Add(item);
    }
}

But, the few required and still serching the solution.

  1. "Merge PDF" multiple files not working

     mergeCommandKey.SetValue("", appPath + " \"%*\"");
    
  2. "Merge PDF" only dispaly when multiple pdf files are selected.

  3. "Merge PDF" start with single instance.

  4. "Open PDF" only dispaly when single pdf file is selected.

  5. "Open PDF" start with single instance.

发布评论

评论列表(0)

  1. 暂无评论