如何在C#中禁用“发送到" [英] how to disable 'send to' in c#

查看:85
本文介绍了如何在C#中禁用“发送到"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

我想制作一个防复制程序,可以在计算机中禁用发送到".当我尝试运行该程序时,发送到"仍然存在.你能告诉我如何禁用发送到"吗?这是程序代码:


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:


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.

推荐答案

我不知道您如何禁用该上下文菜单项,但是我需要指出一些注意事项.

首先,如果您更新注册表,则可能需要重新启动Windows才能获取更改.
其次,catch块不是您只能尝试再次运行同一段代码的地方,它是您处理任何错误的地方,在大多数情况下,您实际上只应捕获能够捕获的异常处理.
第三,在if语句中,检查sendto是否为null.如果不是,则将其设置为null,如果已经将其设置为null,则尝试在其上调用方法,这是愚蠢的.忽略这样一个事实,由于顶层if语句,我们知道在那一点它永远不会为null,这会使代码块完全冗余.

当然,您的代码也不起作用的原因可能是因为您从未真正更改过注册表项的值(假设stat =="disable").

也许这样的事情可能会好一些:
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.

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);
    }
}


这篇关于如何在C#中禁用“发送到"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆