第一步:

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

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就是我们想要的路径

这是获得的完整路径: