11/10/2018, 21:21

[C#] Hướng dẫn tạo Shell Context Menu trên Windows

Xin chào tất cả các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách tạo Shell Context menu trên Windows bằng ngôn ngữ lập trình C# . Ứng dụng này các bạn thường thấy, ví dụ: khi các bạn cài đặt phần mềm nén và giải nén Winrar vào xong. ...

Xin chào tất cả các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách tạo Shell Context menu  trên Windows bằng ngôn ngữ lập trình C#.

Ứng dụng này các bạn thường thấy, ví dụ: khi các bạn cài đặt phần mềm nén và giải nén Winrar vào xong.

Thì khi các bạn right click vào các folder trong window explorer thì sẽ hiển thị các context menu của Winrar.

Trong bài viết này mình sẽ chỉ các bạn cách tích hợp Shell context menu vào nhé.

Ở tạo được context menu, chúng ta sẽ tạo khóa trong Regedit, để thực hiện được công việc này các bạn cần phải chạy ứng dụng dưới quyền Run As Administrator.

Các bạn tham khảo bài viết này: Hướng dẫn chạy ứng dụng dưới quyền Administrator trong lập trình csharp

thì ứng dụng của chúng ta mới có thể tạo khóa xuống regedit được các bạn nhé.

Giao diện demo ứng dụng:

Shell context menu C#

Source code Create Context Menu C#:

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShellContextMenuCsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private bool AddContextMenuItem(string Extension,  string MenuName, string MenuDescription, string MenuCommand)
        {
            bool ret = false;
            RegistryKey rkey =   Registry.ClassesRoot.OpenSubKey(Extension);
            if (rkey != null)
            {
                string extstring = rkey.GetValue("").ToString();
                rkey.Close();
                if (extstring != null)
                {
                    if (extstring.Length > 0)
                    {
                        rkey = Registry.ClassesRoot.OpenSubKey(
                          extstring, true);
                        if (rkey != null)
                        {
                            string strkey = "shell\" + MenuName + "\command";
                            RegistryKey subky = rkey.CreateSubKey(strkey);
                            if (subky != null)
                            {
                                subky.SetValue("", MenuCommand);
                                subky.Close();
                                subky = rkey.OpenSubKey("shell\" +
                                  MenuName, true);
                                if (subky != null)
                                {
                                    subky.SetValue("", MenuDescription);
                                    subky.Close();
                                }
                                ret = true;
                            }
                            rkey.Close();
                        }
                    }
                }
            }
            return ret;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool result = AddContextMenuItem(".zip", "Context Menu Laptrinhvb.net", "Open with &Context Menu Laptrinhvb.net",  Application.ExecutablePath + " %1");
            MessageBox.Show(result.ToString());
        }
    }
}

HAVE FUN :)

Tags: shell context menuright menu windows explorer
0