调试器中显示空值 [英] Null values displayed in debugger

查看:84
本文介绍了调试器中显示空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



调试器中的值显示如下:

FF86_version31.0.1

FF86_nameFirefox

displayFF86versionnull

displayFF86namenull



I have the following code:

The values are displayed as follows in the debugger:
FF86_version "31.0.1"
FF86_name "Firefox"
displayFF86version "null"
displayFF86name "null"

public static bool checkFF86version(string FF86_name, string FF86_version)
{
    RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    .OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
    //.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    string displayFF86version;
    string displayFF86name;

    if (key != null)
    {
        foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
        {
            displayFF86name = subkey.GetValue("DisplayName") as string;
            if (displayFF86name != null && displayFF86name.Contains(FF86_name))
            {
                displayFF86version = subkey.GetValue("DisplayVersion") as string;

            if (displayFF86version.Equals(FF86_version))
                {
                    var version = displayFF86version; //Comes from the Registry
                    var parsedversion = Version.Parse(version);

                    var minimumversion = new Version(FF86_version); //Static Version Check

                    if (parsedversion >= minimumversion)
                        return true;
                }
            }
            return false;
        }
    }
    return false;





我想看看是否安装了Firefox。如果检测到,则比较DisplayVersion以查看它是否>或者<标识如下所示的版本:





I am trying to see if "Firefox" is installed. If it is detected compare the DisplayVersion to see if it is > or < the version identified as displayed below:

if (checkFF86version("Firefox", "31.0.1"))
   listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox is Installed and is the latest version" });
else
    listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox needs updated" });







我没有收到任何NullReferenceException并且代码运行得很好,但它是从关键字符串生成NULL数据,就像它没有正确循环一样,如上所述。任何人都可以提供的帮助将不胜感激。



谢谢!




I am not receiving any NullReferenceException's and the code is running perfectly fine, but it is producing "NULL" data from the key strings like it is not looping propperly as stated above. Any help anyone could provide will be greatly appreciated.

Thanks!

推荐答案

一件事;注册表并不总是会在每个程序中包含此信息,因此您应该有一个Else语句来检查注册表中的子键: InstallLocation 如果 DisplayName 不为空,则安装该程序,因此在注册表子键中查找安装路径 InstallLocation ,如果您知道文件名,则使用System.IO fileinfo属性从实际文件构建中获取文件版本。



(这里唯一的问题是知道可执行文件的文件名是什么;因为在注册表子键的Uninstall部分几乎不可用。)我确信路径存储在注册表中的某个地方...因为你必须要研究或者这里有人会知道。



其次,你在这里使用一个布尔值: if(displayFF86version.Equals(FF86_version))因为等于表示一个布尔检查,以查看它是否与传递给.Equals()的变量匹配,如果该值不存在或不匹配,则始终返回false。您还应该检查 displayFF86version 在上述声明之前是否为空:

One thing; the registry is not always going to contain this information on every program, so you should have an Else statement to check the sub key in the registry: InstallLocation if the DisplayName is not null, then the program is installed, so look for an install path in the registry sub key InstallLocation, and get the file version from the actual file build using System.IO fileinfo properties if you know the file name.

(the only issue you have here is knowing what the file name of the executable is; because that's nearly never available in the Uninstall section of the registry sub keys.) I am sure the path is stored somewhere in the registry... for that you will have to research or maybe someone here will know.

Secondly, you are using a boolean here: if (displayFF86version.Equals(FF86_version)) because the equals represents a Boolean check to see if its a match to the variable you pass to .Equals() which will always return false if the value does not exist or is not a match. You should also check if displayFF86version is not null first prior to the statement above:
if (!(displayFF86version == Null)) {
//Do action here
}

如果它为null,则为文件的可执行文件路径加密 WMI name以获取上面建议的文件版本。这样的东西可能适用于WMI搜索:

and if it is null, then either cypher through WMI for the Executable path for the file name in order to get the file version as suggested above. Something like this might work for WMI searching:

{
	Management.ManagementObjectCollection ManobjectReturn = default(Management.ManagementObjectCollection);
	Management.ManagementObjectSearcher ManobjectSearch = default(Management.ManagementObjectSearcher);
	Management.ManagementObject Manobject = default(Management.ManagementObject);
	ManobjectSearch = new Management.ManagementObjectSearcher("Select * from Win32_Product");
	ManobjectReturn = ManobjectSearch.Get;
	foreach ( Manobject in ManobjectReturn) {
		string Address = Convert.ToString(Manobject("InstallLocation"));
		string NewExeName = Convert.ToString(Manobject("Name"));
		Interaction.MsgBox(string.Format("{0} Location {1} ", Manobject("Name"), Manobject("InstallLocation")));

		//Do your If Checks here for each program. 

	}
}





条件运算符逻辑中的另一件事:



One other thing in your Conditional operator logic:

if (parsedversion >= minimumversion)
                            return true;
                    }



Parsed版本将是文件版本,假设 minimumversion 是为了作为最新的可用版本,您的逻辑应该<=小于或等于;因为您的解析版本永远不会超过可用版本。



除此之外,你的代码看起来还不错。


The Parsed version is going to be the file version, and assuming minimumversion is meant to be the latest available version, your logic should be <= less than or equal to; because your parsed version will never be greater than the available version.

Apart from that, your code looks Ok.


将它改为代码而不是你正在使用的方法。如果你不想改变它们,那么使用我的变量,否则你可以将它们变成你自己的变量。我在这里添加了一些过滤来阻止修补程序和更新,以及从循环中添加等。这些是可选的,可以使用布尔变量启用。当您在调试器中运行它时,您可以在控制台中看到结果,如附图中所示。



此代码经过测试并正常运行。请不要忘记评分,并标记为解决方案。如果您有任何疑问,我很乐意提供帮助。

Alter this into your code instead of the methods you are using. And use my variables or else you can = them to your own, if you are not bothered to change them. I have added some filtering here to stop Hotfixes and updates and alike from being added from the loop. These are optional and can be enabled by using the Boolean variable. You can see the results in your console when you run it in the debugger as seen in the attached image.

This code is tested and working. Please don't forget to rate, and mark as a solution. If you have any questions after you do so; I will be happy to help.
public void GetNameAndVersion()
{
	Microsoft.Win32.RegistryKey iRegKey = null;
	Microsoft.Win32.RegistryKey iSubKey = null;
	string eValue = null;
    string eVersion = null;
	string regpath = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
	iRegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regpath);
	string[] subkeys = iRegKey.GetSubKeyNames();
	bool includes = false;
	foreach (string subk in subkeys) {
		iSubKey = iRegKey.OpenSubKey(subk);
		eValue = Convert.ToString (iSubKey.GetValue("DisplayName", ""));
        eVersion = Convert.ToString(iSubKey.GetValue("DisplayVersion", ""));
		if (!string.IsNullOrEmpty(eValue)) {
			includes = true;
			if (eValue.IndexOf("Hotfix") != -1)
				includes = false;
			if (eValue.IndexOf("Security Update") != -1)
				includes = false;
			if (eValue.IndexOf("Update for") != -1)
				includes = true;
			if (includes == true)
				Console.WriteLine ( eValue );
		}
        if (!string.IsNullOrEmpty(eVersion))
        {
            includes = true;
            if (eVersion.IndexOf("Hotfix") != -1)
                includes = false;
            if (eVersion.IndexOf("Security Update") != -1)
                includes = false;
            if (eVersion.IndexOf("Update for") != -1)
                includes = true;
            if (includes == true)
                Console.WriteLine(eVersion);
        }
	}
}



结果:




Result:


我提出了以下解决方案,并想知道我是否能得到每个人都要看看它,看看你是否同意这个解决方案。它完全按照它的说法完成,但我知道还有其他检查可以放入......你的想法!



I have come up with the following solution, and was wondering if I could get it everyone to look at it and see if you agree with this solution. It does exactly as it says, but I know there are additional checks I can put in... Your thoughts!

        public void GetNameAndVersion()
        {
            Microsoft.Win32.RegistryKey iRegKey = null;
            Microsoft.Win32.RegistryKey iSubKey = null;
            string eValue = null;
            string eVersion = null;
            string regpath = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
            iRegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regpath);
            string[] subkeys = iRegKey.GetSubKeyNames();
            bool includes = false;
            foreach (string subk in subkeys)
            {
                iSubKey = iRegKey.OpenSubKey(subk);
                 eValue = Convert.ToString(iSubKey.GetValue("DisplayName", ""));
                 eVersion = Convert.ToString(iSubKey.GetValue("DisplayVersion", ""));

                 if (eValue != null && eValue.Contains("Firefox"))
                 {
                    var version = eVersion;
                     var parsedversion = Version.Parse(version);
                     var minimumversion = new Version("35.0.1");

                     if (parsedversion >= minimumversion)
                         listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox is the latest version or newer" });
                     else if (parsedversion < minimumversion)
                         listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox needs reinstalled" });
                 
                 else
                     includes = false;
                 }
            }
        }
    }
}


这篇关于调试器中显示空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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