Hi all,
Im not sure what the exact term should i called. I want to add shortcut to my C# program when i right click in windows.
From my findings, it got something to do with configure the "regedit". I have this example, but it was made for IE. can anyone point me to any references that can solve my problems?
references:
http://blog.voidnish.com/?p=17 http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/
thank you very much.
UPDATED today..
Based on response from Factor Mystic, i add this code to the original. I have 2 solutions. One, It was created in registry HKEY_ CLASSES_ ROOT, but i cannot see the result when i right click the doc files.
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();
}
}
Second, create in HKEY_ LOCAL_ MACHINE.
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;
}
}
My concerned, which Main Key should i use?