Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用字典和循环VB.net的最佳实践_Vb.net_.net 2.0_Vb.net 2010 - Fatal编程技术网

使用字典和循环VB.net的最佳实践

使用字典和循环VB.net的最佳实践,vb.net,.net-2.0,vb.net-2010,Vb.net,.net 2.0,Vb.net 2010,我正在尝试编写一个程序来检查注册表中windows服务的开始值,我正在使用一个字典(字符串,点)来存储注册表中服务的名称(字符串),并且开始值(点)的默认值和真实值X为默认值,Y为真值 Private _defaultRegistryVals As New Dictionary(Of String, Point) Public Enum StartupMode Disabled = 0 Manual = 1 Auto = 2 Au

我正在尝试编写一个程序来检查注册表中windows服务的开始值,我正在使用一个字典(字符串,点)来存储注册表中服务的名称(字符串),并且开始值(点)的默认值和真实值X为默认值,Y为真值

Private _defaultRegistryVals As New Dictionary(Of String, Point)

Public Enum StartupMode
        Disabled = 0
        Manual = 1
        Auto = 2
        AutoDelayed = 3
    End Enum

Private Function Analyze as Boolean
Dim mostOfRegistryPath As String = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"
 If Not _HasLoaded then

_defaultRegistryVals.Add("AxInstSV", New Point(StartupMode.Manual, 0)) 
_defaultRegistryVals.Add("SensrSvc", New Point(StartupMode.Manual, 0))
_defaultRegistryVals.Add("AeLookupSvc", New Point(StartupMode.Manual, 0))

_HasLoaded = True

End If

For Each kvp As KeyValuePair(Of String, Point) In _defaultRegistryVals

Try 

kvp.Value.Y = TryCast( My.Computer.Registry.GetValue (mostOfRegistryPath + kvp.Key),      "Start", Nothing), double)
Catch ex As Exception

End Try

Next kvp 
既然字典是只读的,我的程序从注册表中获取每个Windows服务的起始值的真实值的最佳方法是什么。我应该让它在添加字典条目时检查每个值吗?还是有更好的办法

这不是全部代码,但它需要一些小技巧才能工作。大多数其他代码只是整个服务列表,以及检查windows版本的按钮,然后开始分析注册表。

请尝试以下代码

    Dim DictionaryKeys1(_defaultRegistryVals.Count - 1) As String

    _defaultRegistryVals.Keys.CopyTo(DictionaryKeys1, 0)

    For Each KeyValue In DictionaryKeys1
        Try
            Dim RegistryValue1 As String = CStr(My.Computer.Registry.GetValue(mostOfRegistryPath + KeyValue, "Start", Nothing))

            If Not RegistryValue1 Is Nothing Then
                Dim Point1 As New Point

                Point1.X = _defaultRegistryVals(KeyValue).X
                Point1.Y = CInt(RegistryValue1)

                _defaultRegistryVals(KeyValue) = Point1
            End If
        Catch ex As Exception
            MsgBox("Error")
        End Try
    Next

你为什么认为字典是只读的?从MSDN中,
获取或设置与指定键关联的值
。它基本上告诉我不能在循环中更改该值。这是我得到@neolik的错误,我的代码得到的错误在两个Imgur图片链接中,您需要重新创建您的值并重新分配-请参阅下面已经涵盖的答案。