2009-09-28 86 views
0

林不知道確切的術語,我應該叫什麼。我想在我的C#程序中添加快捷方式,當我在Windows中右鍵單擊。添加快捷方式到我的程序時,右鍵單擊

從我的研究結果,它得到的東西做配置「註冊表編輯器」。我有這個例子,但它是爲IE編寫的。任何人都可以指出我可以解決我的問題的任何參考?

引用:

http://blog.voidnish.com/?p=17 http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/

非常感謝你。

今天更新..

基於從因子神祕的反應,我將此代碼添加到原來的。我有2個解決方案。一,它在註冊表HKEY_ CLASSES_ ROOT被創造了,但我不能看到結果時,我右鍵單擊文檔文件。

private const string ProgName = "Software\\Classes\\Word.Document\\shell"; 
private const string MenuName = "Software\\Classes\\Word.Document\\shell\\NewTesting"; 
public const string Command =Software\\Classes\\Word.Document\\shell\\NewTesting\\command"; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     txtProgram.Text = "Word.Document.8"; 
     txtName.Text = "Testing"; 
     txtPath.Text = "C:\\temp\\encriptTest.exe"; 
     check(); 
     addItem() 
    } 
    public void check() 
    { 
     RegistryKey regmenu = null; 
     RegistryKey regcmd = null; 
     try 
     { 
      //this.CheckSecurity(); 
      regmenu = Registry.ClassesRoot.OpenSubKey(MenuName, false); 
     } 
     catch (ArgumentException ex) 
     { 
      // RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList. 
      MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess. " 
       + "AllAccess cannot be used as a parameter in GetPathList because it represents more than one " 
       + "type of registry variable access : \n" + ex); 
     } 
     catch (SecurityException ex) 
     { 
      // RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList. 
      MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess. " + ex); 
      this.btnAddMenu.Enabled = false; 
      //this.btnRemoveMenu.Enabled = false; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(this, ex.ToString()); 
     } 
     finally 
     { 
      if (regmenu != null) 
       regmenu.Close(); 
      if (regcmd != null) 
       regcmd.Close(); 
     } 
    } 

    private void CheckSecurity() 
    { 
     //check registry permissions 
     RegistryPermission regPerm; 
     regPerm = new RegistryPermission(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + ProgName); 
     regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + MenuName); 
     regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + Command); 
     regPerm.Demand(); 
    } 

    private void addItem() 
    { 
     RegistryKey regmenu = null; 
     RegistryKey regcmd = null; 
     RegistryKey regprog = null; 
     try 
     { 
      regprog = Registry.ClassesRoot.CreateSubKey(ProgName); 
      if (regmenu != null) 
       regmenu.SetValue("", this.txtProgram.Text); 
      regmenu = Registry.ClassesRoot.CreateSubKey(MenuName); 
      if (regmenu != null) 
       regmenu.SetValue("", this.txtName.Text); 
      regcmd = Registry.ClassesRoot.CreateSubKey(Command); 
      if (regcmd != null) 
       regcmd.SetValue("", this.txtPath.Text); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(this, ex.ToString()); 
     } 
     finally 
     { 
      if (regprog != null) 
       regprog.Close(); 
      if (regmenu != null) 
       regmenu.Close(); 
      if (regcmd != null) 
       regcmd.Close(); 
     }  
    } 

其次,創建HKEY_ LOCAL_機。

private bool Add_Item(string Extension,string MenuName, string MenuDescription, string MenuCommand) 
    { 
     //receive .doc,OpenTest,Open with Opentest,path: C:\\temp\\encriptTest.exe %1 
     bool ret = false; 
     RegistryKey rkey = //receive .doc extension (word.Document.8) 
      Registry.ClassesRoot.OpenSubKey(Extension); //set HKEY_LOCAL_MACHINE\software\classes\word.Document.8 
     if (rkey != null) 
     { 
      string extstring = rkey.GetValue("").ToString(); 
      rkey.Close(); 
      if (extstring != null) 
      { 
       if (extstring.Length > 0) 
       { 
        rkey = Registry.ClassesRoot.OpenSubKey(extstring, true); 
        if (rkey != null) //with extension file receive OpenTest as shell 
        { 
         string strkey = "shell\\" + MenuName + "\\command"; //..\shell\OpenTest\command 
         RegistryKey subky = rkey.CreateSubKey(strkey); 
         if (subky != null) 
         { 
          subky.SetValue("", MenuCommand); // path: C:\\temp\\encriptTest.exe %1 
          subky.Close(); 
          subky = rkey.OpenSubKey("shell\\" + MenuName, true); //..\shell\OpenTest 
          if (subky != null) 
          { 
           subky.SetValue("", MenuDescription); // name displayed: Open with &OpenTest 
           subky.Close(); 
          } 
          ret = true; 
         } 
         rkey.Close(); 
        } 
       } 
      } 
     } 
     return ret; 
    } 
} 

我關心的是哪個主鍵?

+0

你是說你想在Windows資源管理器中右鍵點擊你的實際程序exe文件嗎?用鼠標右鍵單擊與您的程序關聯的文件類型? – 2009-09-28 03:34:58

+0

嗨,Eric,Tom Frey是對的!我有一個C#程序,我想添加我的程序在列表中的一個,當我在Windows中右鍵單擊。對不起,混亂! – user147685 2009-09-28 04:28:44

回答

0

非常感謝您的答覆。Very2 apreciate他們..

作爲按照conlcusion,解決我的概率3種方法。在易understadable方法:

添加快捷方式有3種方式:

1.直接在註冊表窗口創建:僅適用於文件夾 http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/

2.快捷。 http://www.codeproject.com/KB/cs/appendmenu.aspx http://blog.voidnish.com/?p=17

提供給所有文件和文件夾

3.快捷。 http://www.autoitscript.com/forum/index.php?showtopic=103265&view=findpost&p=731920

0

你將要確定的文件類型(進程id) .doc文件。您可以在HKEY_CURRENT_USER\Software\Classes\.doc發現這個(它是默認值)。

然後添加密鑰HKEY_CURRENT_USER\Software\Classes\<ProgID>\shell\NewMenuOption\command,其中默認值是程序的路徑。

你可以用Registry.SetValueGetValue來完成這一切。

Check out this msdn page to get started.

編輯:附加信息,配置單元密鑰之間的差:

HKEY_LOCAL_MACHINE\Software\ClassesHKEY_CURRENT_USER\Software\Classes相似,但HKLM爲系統默認/所有用戶設置,並HKCU爲每個用戶設置。按照用戶設置不需要提升權限,所以你可以寫你的上下文菜單鍵作爲具有無痛苦普通用戶。

HKEY_CLASSES_ROOT是組合HKEY_LOCAL_MACHINE\Software\ClassesHKEY_CURRENT_USER\Software\Classes的視圖,其中寫入指向HKLM。這是編寫系統默認值的一個捷徑,很多教程都展示了這一點,因爲稍微有些更簡單,但除非您爲所有用戶安裝應用程序,否則我不推薦它。

Advanced registry info on MSDN

+0

有點混淆。主鍵之間有什麼不同?我應該放哪一個? current_user,class_root,local_machine?如何確定使用哪一個? thax提前 – user147685 2009-09-29 04:25:55

相關問題