日期:2014-05-18  浏览次数:20778 次

大家帮忙看个C#操作注册表的问题
我下边这个代码是一个很简单的修改IE9进程数的代码,就是在Software\Microsoft\Internet Explorer\Main下添加一个TabProcGrowth键值。问题出现在:当我添加了如下代码中的注释部分以后,程序就先输出不存在那部分,直到最后一个才输出存在。。

C# code
using System;
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IE进程管理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnChangeIEProcessNum_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.CurrentUser;
            RegistryKey ieProcessNum = key.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main", true);
            string[] valueName;
            valueName = ieProcessNum.GetValueNames();
            foreach (string s in valueName)
            {

                if (s == "TabProcGrowth")
                {
                    
                    MessageBox.Show("键值存在", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (s != "TabProcGrowth")//添加这部分后,程序就一直提示键值不存在,直到最后才输出键值存在。
                {
                    MessageBox.Show("键值不存在,要创建吗?", "提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    return;
                }
           }
        }

    }
}


------解决方案--------------------
C# code

bool bFound = false;
foreach (string s in valueName)
            {

                if (s == "TabProcGrowth")
                {
                    bFound = true;
                    break;   
                 }
             }
if (bFound)
{
// 有
}
else
{
// 木有
}