C# 如何向shell文件扩展名添加更多命令?

C# 如何向shell文件扩展名添加更多命令?,c#,.net,winforms,contextmenu,C#,.net,Winforms,Contextmenu,我想做的是添加两个上下文菜单,“复制到灰度”和“列出所有图像” 我传递一个参数“List”/“Copy”来区分它们 string menuCommand = string.Format("\"{0}\"{1}\" \"%L\" CopyGrayscaleImage", " List", Application.ExecutablePath); 但这是错误的 menuCommand行应该是什么样子,以及在Main方法中如何处理它 这是FileShellExtensions类 using Syst

我想做的是添加两个上下文菜单,“复制到灰度”和“列出所有图像”

我传递一个参数“List”/“Copy”来区分它们

string menuCommand = string.Format("\"{0}\"{1}\" \"%L\" CopyGrayscaleImage", " List", Application.ExecutablePath);
但这是错误的

menuCommand行应该是什么样子,以及在Main方法中如何处理它

这是FileShellExtensions类

using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace SimpleContextMenu
{
    /// <summary>
    /// Register and unregister simple shell context menus.
    /// </summary>
    static class FileShellExtension
    {
        /// <summary>
        /// Register a simple shell context menu.
        /// </summary>
        /// <param name="fileType">The file type to register.</param>
        /// <param name="shellKeyName">Name that appears in the registry.</param>
        /// <param name="menuText">Text that appears in the context menu.</param>
        /// <param name="menuCommand">Command line that is executed.</param>
        public static void Register(
            string fileType, string shellKeyName, 
            string menuText, string menuCommand)
        {
            Debug.Assert(!string.IsNullOrEmpty(fileType) &&
                !string.IsNullOrEmpty(shellKeyName) &&
                !string.IsNullOrEmpty(menuText) && 
                !string.IsNullOrEmpty(menuCommand));

            // create full path to registry location
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            // add context menu to the registry
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))
            {
                key.SetValue(null, menuText);
            }

            // add command that is invoked to the registry
            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(
                string.Format(@"{0}\command", regPath)))
            {               
                key.SetValue(null, menuCommand);
            }
        }

        /// <summary>
        /// Unregister a simple shell context menu.
        /// </summary>
        /// <param name="fileType">The file type to unregister.</param>
        /// <param name="shellKeyName">Name that was registered in the registry.</param>
        public static void Unregister(string fileType, string shellKeyName)
        {
            Debug.Assert(!string.IsNullOrEmpty(fileType) &&
                !string.IsNullOrEmpty(shellKeyName));

            // full path to the registry location           
            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);

            // remove context menu from the registry
            Registry.ClassesRoot.DeleteSubKeyTree(regPath);
        }
    }

}
使用系统;
使用系统诊断;
使用Microsoft.Win32;
名称空间SimpleContext菜单
{
/// 
///注册和取消注册简单shell上下文菜单。
/// 
静态类fileshell扩展
{
/// 
///注册一个简单的shell上下文菜单。
/// 
///要注册的文件类型。
///出现在注册表中的名称。
///显示在关联菜单中的文本。
///执行的命令行。
公开静态无效登记册(
字符串文件类型,字符串shellKeyName,
字符串菜单文本、字符串菜单命令)
{
Debug.Assert(!string.IsNullOrEmpty(文件类型)&&
!string.IsNullOrEmpty(shellKeyName)&&
!string.IsNullOrEmpty(menuText)和
!string.IsNullOrEmpty(menuCommand));
//创建注册表位置的完整路径
string regPath=string.Format(@“{0}\shell\{1}”,文件类型,shellKeyName);
//将上下文菜单添加到注册表
使用(RegistryKey key=Registry.ClassesRoot.CreateSubKey(regPath))
{
key.SetValue(null,menuText);
}
//添加调用到注册表的命令
使用(RegistryKey key=Registry.ClassesRoot.CreateSubKey)(
string.Format(@“{0}\command”,regPath)))
{               
key.SetValue(null,menuCommand);
}
}
/// 
///取消注册一个简单的shell上下文菜单。
/// 
///要注销的文件类型。
///在注册表中注册的名称。
公共静态无效注销(字符串文件类型、字符串shellKeyName)
{
Debug.Assert(!string.IsNullOrEmpty(文件类型)&&
!string.IsNullOrEmpty(shellKeyName));
//注册表位置的完整路径
string regPath=string.Format(@“{0}\shell\{1}”,文件类型,shellKeyName);
//从注册表中删除上下文菜单
Registry.ClassesRoot.DeleteSubKeyTree(regPath);
}
}
}
这是Progrsm.cs

using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

[assembly: CLSCompliant(true)]
namespace SimpleContextMenu
{
    static class Program
    {
        // file type to register
        const string FileType = "jpegfile";

        // context menu name in the registry
        const string KeyName = "Simple Context Menu";
        const string KeyName1 = "Simple Context Menu1";

        // context menu text
        const string MenuText = "Copy to Grayscale";
        const string MenuText1 = "List all images";

        [STAThread]
        static void Main(string[] args)
        {
            // process register or unregister commands
            if (!ProcessCommand(args))
            {
                // invoked from shell, process the selected file
                CopyGrayscaleImage(args[0]);
            }
        }

        /// <summary>
        /// Process command line actions (register or unregister).
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <returns>True if processed an action in the command line.</returns>
        static bool ProcessCommand(string[] args)
        {
            // register
            if (args.Length == 0 || string.Compare(args[0], "-register", true) == 0)
            {
                // full path to self, %L is placeholder for selected file
                //string menuCommand = string.Format(
                    //"\"{0}\" \"%L\"", Application.ExecutablePath);
                string menuCommand = string.Format("\"{0}\"{1}\" \"%L\" CopyGrayscaleImage", " List", Application.ExecutablePath);

                // register the context menu
                FileShellExtension.Register(Program.FileType,
                    Program.KeyName, Program.MenuText,
                    menuCommand);

                FileShellExtension.Register(Program.FileType,
                    Program.KeyName1, Program.MenuText1,
                    menuCommand);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was registered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // unregister       
            if (string.Compare(args[0], "-unregister", true) == 0)
            {
                // unregister the context menu
                FileShellExtension.Unregister(Program.FileType, Program.KeyName);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was unregistered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // command line did not contain an action
            return false;
        }

        /// <summary>
        /// Make a grayscale copy of the image.
        /// </summary>
        /// <param name="filePath">Full path to the image to copy.</param>
        static void CopyGrayscaleImage(string filePath)
        {
            try
            {
                // full path to the grayscale copy
                string grayFilePath = Path.Combine(
                    Path.GetDirectoryName(filePath),
                    string.Format("{0} (grayscale){1}",
                    Path.GetFileNameWithoutExtension(filePath),
                    Path.GetExtension(filePath)));

                // using calls Dispose on the objects, important 
                // so the file is not locked when the app terminates
                using (Image image = new Bitmap(filePath))
                using (Bitmap grayImage = new Bitmap(image.Width, image.Height))
                using (Graphics g = Graphics.FromImage(grayImage))
                {
                    // setup grayscale matrix
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorMatrix(new ColorMatrix(new float[][]{   
                        new float[]{0.3086F,0.3086F,0.3086F,0,0},
                        new float[]{0.6094F,0.6094F,0.6094F,0,0},
                        new float[]{0.082F,0.082F,0.082F,0,0},
                        new float[]{0,0,0,1,0,0},
                        new float[]{0,0,0,0,1,0},
                        new float[]{0,0,0,0,0,1}}));

                    // create the grayscale image
                    g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
                        0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr);

                    // save to the file system
                    grayImage.Save(grayFilePath, ImageFormat.Jpeg);

                    // success
                    MessageBox.Show(string.Format("Copied grayscale image {0}", grayFilePath), Program.KeyName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), Program.KeyName);
                return;
            }
        }
    }
}
使用系统;
使用System.Windows.Forms;
使用System.IO;
使用系统图;
使用系统、绘图、成像;
[大会:CLSCompliant(true)]
名称空间SimpleContext菜单
{
静态类程序
{
//要注册的文件类型
常量字符串FileType=“jpegfile”;
//注册表中的上下文菜单名称
const string KeyName=“简单上下文菜单”;
const string KeyName1=“简单上下文菜单1”;
//上下文菜单文本
常量字符串MenuText=“复制到灰度”;
const string MenuText1=“列出所有图像”;
[状态线程]
静态void Main(字符串[]参数)
{
//处理注册或取消注册命令
如果(!ProcessCommand(args))
{
//从shell调用,处理所选文件
CopyGrayscaleImage(args[0]);
}
}
/// 
///处理命令行操作(注册或取消注册)。
/// 
///命令行参数。
///如果在命令行中处理操作,则为True。
静态bool ProcessCommand(字符串[]args)
{
//登记册
if(args.Length==0 | | string.Compare(args[0],“-register”,true)==0)
{
//自我的完整路径,%L是所选文件的占位符
//字符串菜单命令=string.Format(
//“\”{0}\“\%L\”,Application.ExecutablePath);
string menuCommand=string.Format(“\”{0}\“{1}\”\%L\“CopyGrayscaleImage”、“List”、Application.ExecutablePath);
//注册上下文菜单
FileShellExtension.Register(Program.FileType,
Program.KeyName、Program.MenuText、,
菜单命令);
FileShellExtension.Register(Program.FileType,
Program.KeyName1,Program.MenuText1,
菜单命令);
MessageBox.Show(string.Format(
“{0}外壳程序扩展已注册。”,
Program.KeyName),Program.KeyName);
返回true;
}
//注销
if(string.Compare(args[0],“-unregister”,true)==0)
{
//取消注册上下文菜单
取消注册(Program.FileType,Program.KeyName);
MessageBox.Show(string.Format(
“{0}外壳程序扩展已注销。”,
Program.KeyName),Program.KeyName);
返回true;
}
//命令行不包含操作
返回false;
}
/// 
///制作图像的灰度副本。
/// 
///要复制的图像的完整路径。
静态void CopyGrayscaleImage(字符串文件路径)
{
尝试
{
//灰度副本的完整路径
字符串grayFilePath=Path.Combine(
GetDirectoryName(文件路径),
格式(“{0}(灰度){1}”,
Path.GetFileNameWithoutExtension(filePath),
GetExtension(filePath));
//对对象使用Dispose调用,这很重要
//因此,当应用程序终止时,文件不会被锁定
使用(图像=新位图(文件路径))
使用(Bitma)
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

[assembly: CLSCompliant(true)]
namespace SimpleContextMenu
{
    static class Program
    {
        // file type to register
        const string FileType = "bitmapfile";//"jpegfile";

        // context menu name in the registry
        const string KeyName = "Simple Context Menu";
        const string KeyName1 = "Simple Context Menu1";

        // context menu text
        const string MenuText = "Copy to Grayscale";
        const string MenuText1 = "Resize all images";

        [STAThread]
        static void Main(string[] args)
        {
            System.Threading.Thread.Sleep(30000);
            // process register or unregister commands
            if (!ProcessCommand(args))
            {
                string action = args[0];
                MessageBox.Show(action);
                string fileName = args[1];

                if (action == "Copy")
                {
                    // invoked from shell, process the selected file
                    CopyGrayscaleImage(fileName);
                }
                else if (action == "Resize")
                {
                    string FilePath = Path.Combine(
                    Path.GetDirectoryName(fileName),
                    string.Format("{0} (resized){1}",
                    Path.GetFileNameWithoutExtension(fileName),
                    Path.GetExtension(fileName)));
                    MessageBox.Show(FilePath);
                    Bitmap bmp1 = new Bitmap(ResizeImages(FilePath,100,100));
                    bmp1.Save(FilePath);
                    bmp1.Dispose();
                }
            }
        }

        /// <summary>
        /// Process command line actions (register or unregister).
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <returns>True if processed an action in the command line.</returns>
        static bool ProcessCommand(string[] args)
        {
            // register
            if (args.Length == 0 || string.Compare(args[0], "-register", true) == 0)
            {
                // full path to self, %L is placeholder for selected file
                string menuCommand = string.Format("\"{0}\" Copy \"%L\"", Application.ExecutablePath);

                // register the context menu
                FileShellExtension.Register(Program.FileType,
                    Program.KeyName, Program.MenuText,
                    menuCommand);

                string menuCommand1 = string.Format("\"{0}\" Resize \"%L\"", Application.ExecutablePath);

                FileShellExtension.Register(Program.FileType,
                    Program.KeyName1, Program.MenuText1,
                    menuCommand1);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was registered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // unregister       
            if (string.Compare(args[0], "-unregister", true) == 0)
            {
                // unregister the context menu
                FileShellExtension.Unregister(Program.FileType, Program.KeyName);

                MessageBox.Show(string.Format(
                    "The {0} shell extension was unregistered.",
                    Program.KeyName), Program.KeyName);

                return true;
            }

            // command line did not contain an action
            return false;
        }

        /// <summary>
        /// Make a grayscale copy of the image.
        /// </summary>
        /// <param name="filePath">Full path to the image to copy.</param>
        static void CopyGrayscaleImage(string filePath)
        {
            try
            {
                // full path to the grayscale copy
                string grayFilePath = Path.Combine(
                    Path.GetDirectoryName(filePath),
                    string.Format("{0} (grayscale){1}",
                    Path.GetFileNameWithoutExtension(filePath),
                    Path.GetExtension(filePath)));

                // using calls Dispose on the objects, important 
                // so the file is not locked when the app terminates
                using (Image image = new Bitmap(filePath))
                using (Bitmap grayImage = new Bitmap(image.Width, image.Height))
                using (Graphics g = Graphics.FromImage(grayImage))
                {
                    // setup grayscale matrix
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorMatrix(new ColorMatrix(new float[][]{   
                        new float[]{0.3086F,0.3086F,0.3086F,0,0},
                        new float[]{0.6094F,0.6094F,0.6094F,0,0},
                        new float[]{0.082F,0.082F,0.082F,0,0},
                        new float[]{0,0,0,1,0,0},
                        new float[]{0,0,0,0,1,0},
                        new float[]{0,0,0,0,0,1}}));

                    // create the grayscale image
                    g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
                        0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr);

                    // save to the file system
                    grayImage.Save(grayFilePath, ImageFormat.Jpeg);

                    // success
                    MessageBox.Show(string.Format("Copied grayscale image {0}", grayFilePath), Program.KeyName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An error occurred: {0}", ex.Message), Program.KeyName);
                return;
            }
        }

        private static Bitmap ResizeImages(String filename, int maxWidth, int maxHeight)
        {
            using (Image originalImage = Image.FromFile(filename))
            {
                //Caluate new Size
                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
                if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                {
                    newWidth = maxWidth;
                    newHeight = (int)Math.Round(newWidth / aspectRatio);
                }
                else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                {
                    newHeight = maxHeight;
                    newWidth = (int)Math.Round(newHeight * aspectRatio);
                }
                if (newWidth >= 0 && newHeight >= 0)
                {
                    Bitmap newImage = new Bitmap(newWidth, newHeight);
                    using (Graphics g = Graphics.FromImage(newImage))
                    {
                        //--Quality Settings Adjust to fit your application
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
                        return newImage;
                    }
                }
                return null;
            }
        }
    }
}
string menuCommand = string.Format("\"{0}\" Copy \"%L\"", Application.ExecutablePath);

// register the context menu
FileShellExtension.Register(Program.FileType,
                Program.KeyName, Program.MenuText,
                menuCommand);

string menuCommand1 = string.Format("\"{0}\" List \"%L\"", Application.ExecutablePath);

// register the context menu 1
FileShellExtension.Register(Program.FileType,
                Program.KeyName1, Program.MenuText1,
                menuCommand1);
static void Main(string[] args)
{
    // process register or unregister commands
    if (!ProcessCommand(args))
    {
        string action = args[0];
        MessageBox.Show(action);
        string fileName = args[1];

        if (action == "Copy")
        {
            // invoked from shell, process the selected file
            CopyGrayscaleImage(fileName);
        }
        else if (action == "List")
        {
            ListImage(fileName); 
        }
    }
}