首页 > 其他 > 详细

程序添加右键菜单运行并传右键文件全名

时间:2015-12-08 00:24:38      阅读:208      评论:0      收藏:0      [点我收藏+]

第一步:

首先我们注册程序到注册表

Registry.SetValue("HKEY_LOCAL_MACHINE\\Software\\Classes\\Directory\\Shell\\New Window", "", "你好");

//添加文件夹的右键菜单,名字叫“你好”

            Registry.SetValue("HKEY_LOCAL_MACHINE\\Software\\*\\Directory\\Shell\\你好", "", "你好");

//添加所有类型文件的右键菜单,名字叫“你好”

     Registry.SetValue("HKEY_LOCAL_MACHINE\\Software\\Classes\\Directory\\Shell\\New Window\\Command", "", @"C:\Users\xiaoxin\Desktop\AddRightMenu\bin\Debug\AddRightMenu.exe %1");

//设置打开命令,注意%1是传递参数的

//@"C:\Users\xiaoxin\Desktop\AddRightMenu\bin\Debug\AddRightMenu.exe  是程序文件路径

 

            Registry.SetValue("HKEY_LOCAL_MACHINE\\Software\\Classes\\*\\Shell\\你好\\Command", "", @"C:\Users\xiaoxin\Desktop\AddRightMenu\bin\Debug\AddRightMenu.exe %1");

//设置打开命令,注意%1是传递参数的

//@"C:\Users\xiaoxin\Desktop\AddRightMenu\bin\Debug\AddRightMenu.exe  是程序文件路径

MessageBox.Show("右键菜单添加成功");

 

 

这段代码可以写在任意地方,可以按钮响应事件,From _Load也可以

 

这是效果

技术分享

 

 技术分享

 

 

 

第二步:

 

因为传进参数了通过System.Diagnostics.Process.Start("你的程序.exe 参数1"),所以必须接受参数,然而C#只有main函数才能接受参数,所以我们必须更改程序入口;

 

 

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

我们只需改成这样:

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length == 0)
        Application.Run(new Form1());
    else
        Application.Run(new Form1(args));
}

Form1窗体的构造:

string[] args=null;
public Form1()
{
    InitializeComponent();
}
public Form1(string[] args)
{
    InitializeComponent();
    this.args = args;

 

 

 

改造完成那么,args就是我们想要的路径

这是获得的完整路径:

技术分享

 

程序添加右键菜单运行并传右键文件全名

原文:http://www.cnblogs.com/madai/p/5027902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!