I don't know how you'd disable that context menu item but there are a few things I need to point out.
First, if your updating the registry you'll likely need to restart windows before the changes get picked up.
Second, the
catch
block is not a place where you can just try to run the same piece of code again, it is there for you to handle any errors, and in most situations you should really only catch the exceptions that you are able to handle.
Third, in your
if
statements you check if
sendto
is
null
. If it isn't you set it to null and if it's null already then you try to call a method on it, which is foolish. Ignoring the fact that because of the top level
if
statement we know that it will never be null at that point anyway which makes the chunk of code completely redundant.
EDIT: Also of course the reason your code isn't working could be because you never actually change the value of the registry key (assuming stat == "disable").
Perhaps something like this may fare a little better:
private void sendto(string stat)
{
try
{
RegistryKey sendto = Registry.ClassesRoot.OpenSubKey("AllFilesystemObjects\\shellex\\ContextMenuHandlers\\SendTo", true);
if(sendto == null)
{
sendto = Registry.ClassesRoot.OpenSubKey("AllFilesystemObjects\\shellex\\ContextMenuHandlers\\Send To", true);
}
if (sendto != null)
{
if (stat == "disable")
sendto.SetValue(null, "");
else
sendto.SetValue(null, "{7BA4C740-9E81-11CF-99D3-00AA004AE837}");
}
else
MessageBox.Show("Specified Registry key was not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(SecurityException se)
{
MessageBox.Show("User does not have permission to edit the registry key", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}