Visual studio 2013 Listbox添加的条目太多

Visual studio 2013 Listbox添加的条目太多,visual-studio-2013,Visual Studio 2013,我想让这个程序做的是,如果安装了Microsoft Outlook 2010,则在已安装的列表框中显示它,如果未安装,则在未安装的列表框中显示它。listbox1中有一个表单加载时所有已安装应用程序的列表 问题是,虽然它确实适用于已安装的部分,但它在notinstalled框中多次列出了应用程序。我只想让它出现一次 私有子表单1\u Loadsender作为对象,e作为EventArgs处理MyBase.Load Dim regkey, subkey As Microsoft.Win32

我想让这个程序做的是,如果安装了Microsoft Outlook 2010,则在已安装的列表框中显示它,如果未安装,则在未安装的列表框中显示它。listbox1中有一个表单加载时所有已安装应用程序的列表

问题是,虽然它确实适用于已安装的部分,但它在notinstalled框中多次列出了应用程序。我只想让它出现一次

私有子表单1\u Loadsender作为对象,e作为EventArgs处理MyBase.Load

    Dim regkey, subkey As Microsoft.Win32.RegistryKey
    Dim value As String
    Dim regpath As String = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
    regkey = My.Computer.Registry.LocalMachine.OpenSubKey(regpath)
    Dim subkeys() As String = regkey.GetSubKeyNames
    Dim includes As Boolean
    For Each subk As String In subkeys
        subkey = regkey.OpenSubKey(subk)
        value = subkey.GetValue("DisplayName", "")
        If value <> "" Then
            includes = True
            If value.IndexOf("Hotfix") <> -1 Then includes = False
            If value.IndexOf("Security Update") <> -1 Then includes = False
            If value.IndexOf("Update for") <> -1 Then includes = False
            If includes = True Then ListBox1.Items.Add(value)
        End If
    Next

    Dim count As Integer = (ListBox1.Items.Count - 1)
    Dim words As String
    Dim softName As String


    softName = "Microsoft Outlook 2010"
    For a = 0 To count
        words = ListBox1.Items.Item(a)
        If InStr(words.ToLower, softName.ToLower) Then
            Installed.Items.Add(words)
        Else
            NotInstalled.Items.Add(softName)
        End If
    Next

重复字符串是由于调用每个检查项目的NotInstalled.Items.AddsoftName导致的一个简单错误。您可能只想在循环的末尾添加它

不过,您可以使用一点Linq简化代码

Dim result = words.Where(Function(x) x.ToLower().IndexOf("microsoft outlook 2010") >= 0)
if result IsNot Nothing then
    Installed.Items.AddRange(result.ToArray)
else
    NotInstalled.Items.Add(softName)
end if

<>但是你应该考虑代码中的一些问题。如果Outlook是作为Office的一部分安装的,则在“卸载”部分中没有相应的条目。若在64位系统上运行,那个么就会出现自动重定向到注册表不同部分的问题。如果您的Outlook是安装在64位系统上的32位版本,并且您的应用程序以任意CPU模式运行,该怎么办?考虑所有这些可能性不是一件小事。我想提醒你,你应该试试这样的东西。我是c语言的人,所以我希望我的语法是正确的

Dim isOutlookInstalled = False

softName = "Microsoft Outlook 2010"
For a = 0 To count
    words = ListBox1.Items.Item(a)
    If InStr(words.ToLower, softName.ToLower) Then
        Installed.Items.Add(words)
    End If
Next
If isOutlookInstalled <> True
    NotInstalled.Items.Add(softName)
End If

从阅读问题来看,他似乎只想将Outlook添加到两个列表中的任何一个。这会将不是Outlook的所有项目添加到NotInstalled列表中。