第一步:
首先我們注冊程序到注冊表
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就是我們想要的路徑
這是獲得的完整路徑: