Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi.

i want to make an anticopy program that can disable 'send to' in computer. when i try to run the program, the 'send to' is still exist. can you tell me how to disable the 'send to'..? this is the program code:


C#
private void sendto(string stat)
        {   // try and catch: cause window-xp use "Send To" and Vista,Window7 Use "SendTo"
            try
            {
                RegistryKey sendto =
                Registry.ClassesRoot.OpenSubKey("AllFilesystemObjects\\shellex\\ContextMenuHandlers\\SendTo", true);
                if (sendto != null)
                {
                    if (stat == "disable")
                    {
                        if (sendto != null)
                        {
                            sendto = null;
                        }
                        else
                        {
                            sendto.SetValue(null, "");
                        }
                    }
                    else
                    {
                        sendto.SetValue(null, "{7BA4C740-9E81-11CF-99D3-00AA004AE837}");
                    }
                }
            }
            catch
            {
                RegistryKey sendto = Registry.ClassesRoot.OpenSubKey("AllFilesystemObjects\\shellex\\ContextMenuHandlers\\Send To", true);
                if (sendto != null)
                {
                    if (stat == "disable")
                    {
                        if (sendto != null)
                        {
                            sendto = null;
                        }
                        else
                        {
                            sendto.SetValue(null, "");
                        }
                    }
                    else
                    {
                        sendto.SetValue(null, "{7BA4C740-9E81-11CF-99D3-00AA004AE837}");
                    }
                }
            }
        }

much appreciate for your help.
Posted
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 22:44pm    
If you disable any thinkable shell menu, it won't make a copy any harder. :-) Why bothering?
--SA

1 solution

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)
        {
            //Try the other key
            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);
    }
}
 
Share this answer
 
v2
Comments
mrshh 1-Aug-11 23:02pm    
first, i already restart windows. it still the same. still not solve the problem.
second, the catch block is not the same piece as the try block.
can you read the difference?
this is on try:
Registry.ClassesRoot.OpenSubKey("AllFilesystemObjects\\shellex\\ContextMenuHandlers\\SendTo", true);
and this is on catch:
Registry.ClassesRoot.OpenSubKey("AllFilesystemObjects\\shellex\\ContextMenuHandlers\\SendTo", true);
*see the 'send to' to see the difference and read the comment above. // try and catch: cause window-xp use "Send To" and Vista,Window7 Use "SendTo"
*
third, if didnt write the if statements " if sendto is null" it will show "NullReferenceException was unhandled" tats why i need to put the if statement.
fourth, i just want to try to disable send to in c# just to learn. is it a crime..?
btw, thank you for replying. =) much3 help!!
Anthony Mushrow 1-Aug-11 23:32pm    
I see the difference now. It's still a bad idea to use exceptions to do this sort of thing. After a quick check OpenSubKey will just return null if the key is not found, so you'd never even get an exception and try the other key. The only exception you're likely to get is for not having permission to edit the registry.
I've updated my answer with a modified version of your code that should actually work.
mrshh 2-Aug-11 0:14am    
thank you for the code. i try to run it. when i click the button to activate disable send to, it shows "User does not have permission to edit the registry key".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900