C# 比较注册表项以确定其是否大于或小于?
我目前正在使用以下代码:C# 比较注册表项以确定其是否大于或小于?,c#,C#,我目前正在使用以下代码: public static bool checkFF86version(string FF86_version) { RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) .OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Unin
public static bool checkFF86version(string FF86_version)
{
RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
//.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
string displayFF86version;
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayFF86version = subkey.GetValue("DisplayVersion") as string;
if (displayFF86version != null && displayFF86version.Equals(FF86_version))
{
return true;
}
}
key.Close();
}
return false;
}
我想从注册表项中获取DisplayVersion,并检查它是否大于或小于已检查的版本(FF86_版本)
我曾尝试将字符串转换为int,但似乎在null引用上遇到了困难。(如下所示:)
我知道这对你们大多数人来说可能很简单,但出于某种原因,这让我痛苦了两天,我将非常感谢你们的帮助
寻找:
//Check FireFox version and compare it to a known good version
if (!(checkFF86name("Firefox")))
listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox is NOT installed" });
else if (checkFF86name("Firefox") && (checkFF86version("33.0")))
listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox is the latest version" });
else if (checkFF86name("Firefox") && (checkFF86greater("33.0.1")))
listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Firefox is NEWER than checked version" });
else
listView1.Items.Add(new ListViewItem { ImageIndex = 0, Text = "Re-installing...." });
我想我已经尝试过使用以下方法:
var checkedversion = FF86_greater.Split('.');
var installed = displayFF86greater.Split('.');
for (int i = 0; i < installed.Length; i++)
{
var currinstalled = in.Parse(installed[i]);
var currcheckedversion = int.Parse(checkedversion[i]);
if (currinstalled == currcheckedversion)
continue;
if (currinstalled > currcheckedversion)
return true;
if (currinstalled < currcheckedversion)
return false;
你要找的是。然后使用两个
Version
对象进行比较
简单的例子:
// Get string from registry
// RegistryKey.OpenBaseKey(blahblahblah)
var version = "32.0.1"; //Assume this string came from registry
var parsedversion = Version.Parse(version);
var minimumversion = new Version("33.0.1");
if (parsedversion >= minimumversion)
Console.WriteLine("Your version is correct");
else
Console.WriteLine("You need a newer version!");
注意:Parse方法是一种方便的方法;这相当于调用构造函数,也在本例中显示(对于minimumversion
)
除此之外(根据您的代码): 您确实意识到您在这里访问(并迭代)注册表(最坏情况)5次,是吗?它不会杀死你,但它也不会伤害你,只需获得一次值,然后在比较中使用该值 例如:
Version ff86version = GetFF86VersionFromRegistry(); //Implement this method so that it simply returns FF's version as a Version object (or null if the key is not found)
if (ff86version == null)
// FF not installed
else if (ff86version < new Version("33.0")
// FF version less than 33.0 installed
else
// Whatever
Version ff86version=GetFF86VersionFromRegistry()//实现此方法,使其仅将FF的版本作为版本对象返回(如果找不到键,则返回null)
如果(ff86version==null)
//FF未安装
否则如果(FF86版本<新版本(“33.0”)
//已安装低于33.0的FF版本
其他的
//随便
另外,如果你不断重复像“Firefox”这样的东西,它也不会很容易出错。如果你坚持在每个if/else/else中调用该方法,请使用变量/常量。无论我一直返回“False”,该数字的字符串版本目前是什么样子?以下是我正在寻找的:(更新的帖子)删除小数点并转换为整数是行不通的。请将它们分别分为大数、小数和内部版本号(
33
、.0、和1
,对于33.0.1
),将它们分别转换为整数,并正确比较。如果其中一个(例如内部版本)无法从字符串中获取,请为其指定一个默认值(如零)以进行比较。使用我尝试过的代码进行更新,我认为这就是您所指的。顺便感谢您的帮助。我将更新帖子,我不确定为什么我无法在回复中添加更多文本。如果“您一直得到False
”因此,您应该开始调试,只需使用变量检查器/监视器,查看发生了什么。使用调试器逐步检查代码,确保所有变量都包含您希望它们包含的值。另外(@your):请注意key.Close()在很多情况下,因为你简单地返回(例如“退出”方法),所以不会被调用。请考虑使用一个非常有用的信息,我决不是一个专业人员。我试图在黑暗中摸索。
// Get string from registry
// RegistryKey.OpenBaseKey(blahblahblah)
var version = "32.0.1"; //Assume this string came from registry
var parsedversion = Version.Parse(version);
var minimumversion = new Version("33.0.1");
if (parsedversion >= minimumversion)
Console.WriteLine("Your version is correct");
else
Console.WriteLine("You need a newer version!");
if (!(checkFF86name("Firefox")))
...
else if (checkFF86name("Firefox") && (checkFF86version("33.0")))
...
else if (checkFF86name("Firefox") && (checkFF86greater("33.0.1")))
...
else
...
Version ff86version = GetFF86VersionFromRegistry(); //Implement this method so that it simply returns FF's version as a Version object (or null if the key is not found)
if (ff86version == null)
// FF not installed
else if (ff86version < new Version("33.0")
// FF version less than 33.0 installed
else
// Whatever