以下程序,为什么运行后在注册表里却看不到我的程序.  public static bool SetAutoRun(string keyName, string filePath)       
        {
            try
            {
              RegistryKey runKey = Registry.LocalMachine.OpenSubKey Microsoft\Windows\CurrentVersion\Run", true);
                runKey.SetValue(keyName, filePath);
                runKey.Close();
            }
            catch
            {
                return false;
            }
            return true;
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            SetAutoRun("myexe", "d:\\notepad.exe");
            //SetAutoRun("myexe", "d:\notepad.exe");        } 

解决方案 »

  1.   

    RegistryKey runKey = Registry.LocalMachine.OpenSubKey Microsoft\Windows\CurrentVersion\Run", true); 
    这是什么啊
    RegistryKey runKey = Registry.LocalMachine.OpenSubKey("@Microsoft\Windows\CurrentVersion\Run", true); 
    是这样写吧
      

  2.   

    我怎么越看错误越多啊,看我写的对不对啊,仅供参考  public static bool SetAutoRun(string keyName, string filePath)      
            { 
                try 
                { 
                  RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true); 
                   {
    foreach(string site in myRKCN.GetValueNames())
    {
    if(site==keyName)
    {
    myRKCN.DeleteValue(site,false);
    myRKCN.SetValue(keyName,filePath);
    }
    }
                   }
                } 
                catch 
                { 
                    return false; 
                } 
                return true; 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                if (checkBox1.Checked == true) 
                SetAutoRun("myexe", "d:\\notepad.exe"); 
                //SetAutoRun("myexe", "d:\notepad.exe");         } 
      

  3.   

    哈,我有写错了,今天真是邪了,我哪个foreach循环里面的myRKCN就是你的那个runKey
      

  4.   

    因为我在操作注册表中爱myRKCN这样写,倒是忘了你的那个runKey
      

  5.   

    TO mjjzg :改了,但是在注册表Run下还是没的看到notepad.exe
      

  6.   

    不是吧,应该有的,难道没考虑全吗,
    。。、。
    if后再加个
    else
    {
    myRKCN.SetValue(keyName,filePath);
    }
      

  7.   

    To mjjzg老兄,还是不行,你那能调试不,帮下忙,随便找个.EXE文件就可以了.谢谢
      

  8.   

    这是我在winform中试验了一下
    这是代码,是成功的啊 public static void SetAutoRun(string keyName, string filePath)
            {
                try
                {
                    RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true);
                    {
                        //foreach (string site in runKey.GetValueNames())
                        //{
                        //    if (site == keyName)
                        //    {
                        //        runKey.DeleteValue(site, false);
                        //        runKey.SetValue(keyName, filePath);
                        //        MessageBox.Show("添加成功!");
                        //    }
                        //    else
                        //    {
                                runKey.SetValue(keyName, filePath);
                                MessageBox.Show("添加成功!");
                            //}
                        //}
                    }
                }
                catch
                { }
            }         private void button1_Click(object sender, EventArgs e)
            {
                SetAutoRun("myexe", "d:\\notepad.exe"); 
            }同时倒入using Microsoft.Win32;命名空间
    省略的那部分是因为我的注册表run下根本没myexe这一项,所以我也不用不遍历这个目录,直接添加了,
    最后是成功的啊
      

  9.   

    我想问题应该出在你的
    if (checkBox1.Checked == true) 这里,我不明白这是什么,
    也不清楚你为什么要将SetAutoRun方法返回bool类型
    你看着改吧,我试验了3遍都是对的,
    我要下线了,晚上9点以后再上,
    如果有人解答了或你知道怎么回事了那就算了
    如果还不知道,晚上再说或加我MSN:[email protected]
    再见!
      

  10.   

    输出了”添加成功“,但是就是在注册表下没有看到任何东西,但是我用VB写的在注册表里可以看到notepad.exe的,我也不知道为什么?