程序添加右鍵菜單運行並傳右鍵文件全名


第一步:

首先我們注冊程序到注冊表

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就是我們想要的路徑

這是獲得的完整路徑:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM