System.NullReferenceException 中第一次偶然出现的“GetproductCode.exe”类型的异常求解,谢谢

billboywu 2014-06-26 11:49:00
下面这段取注册表ProductCode的代码是网上找的,但是我用VS 2010调试运行时却报
在 System.NullReferenceException 中第一次偶然出现的“GetproductCode.exe”类型的异常

请问是什么原因,我要怎么才能取到ProductCode,谢谢
       
public static string GetProductCode(string displayName)
{
string productCode = string.Empty;
string[] SubKeyNames;
//string[] valueNames;
string displayname;
//string info;
// 如果是32位操作系统,(或者系统是64位,程序也是64位)
string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
// 如果操作系统是64位并且程序是32位的
string bit64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Uninstall = localMachine.OpenSubKey(bit32, true);
SubKeyNames = Uninstall.GetSubKeyNames();

foreach (string subkey in SubKeyNames)
{
RegistryKey productcode = Uninstall.OpenSubKey(subkey);
//valueNames = productcode.GetValueNames();
//foreach (string valueName in valueNames)
//{
// int i = valueName.IndexOf("DisplayName");
// if (i != -1)
// {
// RegistryKey myreg = Uninstall.OpenSubKey();
// info = myreg.GetValue(valueName).ToString();
// MessageBox.Show("结果的值是:" + info);
// }
//}
displayname = productcode.GetValue("DisplayName").ToString();
//if (displayname == displayName)
if (displayname.IndexOf( displayName)!=-1)
{
MessageBox.Show(displayname);

string uninstallString = productcode.GetValue("UninstallString").ToString();
string[] strs = uninstallString.Split(new char[2] { '{', '}' });
productCode = strs[1];
return productCode;
}

}
return productCode;
}

...全文
195 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
billboywu 2014-06-27
  • 打赏
  • 举报
回复
我试下 行不行
billboywu 2014-06-27
  • 打赏
  • 举报
回复
哦,有可能,我的是8.1的系统.
billboywu 2014-06-27
  • 打赏
  • 举报
回复
现在这个代码不会提示了,但是取不到"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"下面的值,取出的值是在 "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";下面的,加上下面这句也不行,请问是什么原因?谢谢 RegistryKey Uninstall = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
 
public static string GetProductCode(string displayName)
        {
            string productCode = string.Empty;
            string[] SubKeyNames;
            string[] valueNames;
            string displayname;
            string info;
            // 如果是32位操作系统,(或者系统是64位,程序也是64位)
            string bit32 = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
            // 如果操作系统是64位并且程序是32位的
            string bit64 = "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
            RegistryKey localMachine = Registry.LocalMachine;
            RegistryKey Uninstall = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
            SubKeyNames = Uninstall.GetSubKeyNames();

            foreach (string subkey in SubKeyNames)
            {
                RegistryKey productcode = Uninstall.OpenSubKey(subkey);
                valueNames = productcode.GetValueNames();
                foreach (string valueName in valueNames)
                {
                    
                    if(valueName=="DisplayName")
                    {
                        RegistryKey myreg = Uninstall.OpenSubKey(subkey);
                        info = myreg.GetValue(valueName).ToString();
                        MessageBox.Show(subkey+"的程序名称是:" + info);
                    }
                }
                    //displayname = productcode.GetValue("DisplayName").ToString();
                    ////if (displayname == displayName)
                    //if (displayname.IndexOf( displayName)!=-1)
                    //{
                    //    MessageBox.Show(displayname);

                //string uninstallString = productcode.GetValue("UninstallString").ToString();
                //string[] strs = uninstallString.Split(new char[2] { '{', '}' });
                //productCode = strs[1];
                //return productCode;
                    //}
                
            }
            return productCode;
        }
poko 2014-06-27
  • 打赏
  • 举报
回复
有一个东西是为null了,但是那个VS不自动提醒,不自动为什么,你可以在觉得可能有错误的代码块上面 包裹一个try {} catch就会提示。
threenewbee 2014-06-26
  • 打赏
  • 举报
回复
调试下,确定哪一行报异常。如果是win7/8,用管理员身份启动VS再调试看看。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧