把程序注册到系统右键菜单,并获取打开的文件地址信息。

         /// <summary>
        /// 启动程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Load(object sender, EventArgs e)
        {
            try
            {
                //获取传递的地址信息
                string paramStr = Environment.CommandLine;//接收点击右键菜单传递的文件地址参数
                string personalFolderPath = paramStr.Substring(paramStr.LastIndexOf(" ")+1);//获取文件路径
            }
            catch
            { }
            finally
            {
                //注册系统右键菜单
                MenuRight("用G-ZIP压缩/解压当前选择项(Z)......");
            }
        }

        /// <summary>
        /// 注册启动程序到系统菜单右键方法
        /// </summary>
        /// <param name="menuName">菜单名</param>
        private void MenuRight(string menuName)
        {
            //注册到文件
            RegistryKey shell1 = Registry.ClassesRoot.CreateSubKey(@"*\shell");
            RegistryKey custom1 = shell1.CreateSubKey(menuName);
            RegistryKey cmd1 = custom1.CreateSubKey("command");
            cmd1.SetValue("", Application.ExecutablePath + " %1");//Application.ExecutablePath 是本程序自身的路径  %1是传入打开的文件路径
            cmd1.Close();
            custom1.Close();
            shell1.Close();
            //注册到目录
            RegistryKey shell = Registry.ClassesRoot.CreateSubKey(@"directory\shell");
            RegistryKey custom = shell.CreateSubKey(menuName);
            RegistryKey cmd = custom.CreateSubKey("command");
            cmd.SetValue("", Application.ExecutablePath + " %1"); //%1 是传入打开的文件路径
            cmd.Close();
            custom.Close();
            shell.Close();
        }
posted @ 2012-05-08 11:47  Yao,Mane  阅读(543)  评论(0编辑  收藏  举报