[C#]建立選單項目

  • 2463
  • 0
  • C#
  • 2014-11-16

[C#]建立選單項目

當我們安裝一些軟體時,windows中右鍵選單就會新增該軟體項目(如果軟體有的話),

如下圖7-zip、TortoiseSVN..等。

目前我專案剛好需要類似功能來執行批次上傳功能,

而透過sharpshell可快速實現(.NET相關解決方案和資源真的很多),

這裡簡單紀錄一下。

image

 

 

下載sharpshell並加入參考

image

 

1.建立實作類別且繼承SharpContextMenu(須設定 ComVisible 屬性),並允許檔案和目錄顯示該項目(依自行需求)

[ComVisible(true)]
    [COMServerAssociation(AssociationType.AllFiles)]//所有檔案   
    public class GetFiles : SharpContextMenu
    {
        protected override bool CanShowMenu()
        {
            return true;
        }

        protected override ContextMenuStrip CreateMenu()
        {
            //建立內容選單項目
            var menu = new ContextMenuStrip();

            //設定項目
            var itemCountLines = new ToolStripMenuItem
            {
                Text = "NFS--批次新增",
                Image = Properties.Resources.Upload
            };

            //增加click event
            itemCountLines.Click += (sender, args) => GetSelectFiles();

            //加入內容選單
            menu.Items.Add(new ToolStripSeparator());           
            menu.Items.Add(itemCountLines);
            menu.Items.Add(new ToolStripSeparator());
          
            return menu;
        }

        private void GetSelectFiles()
        {           
            var sb = new StringBuilder();

            //  Go through each file.
            foreach (var filePath in SelectedItemPaths)
            {
                sb.AppendLine(filePath);                
            }

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = @"E:\myexe\metroui.exe";         
            Process.Start(startInfo);
        }
    }
由於需要讓使用者選擇文管中心上傳目錄,所以這裡我打算呼叫上傳程式。
 
先透過 ServerManager.exe 測試 COM元件是否執行正常

image

該工具很方便,可以很直覺看出是否安裝和註冊了。
 
選擇測試

image

 
結果

image

 
click後呼叫上傳程式

image

 

 

 

參考

sharpshell

部署 Interop 應用程式