我正在尝试将一些 VB.net 代码转换为 C#,并且在尝试转换这一行时遇到问题。
VB.NET
Dim myreg As Microsoft.Win32.Registry
我知道它是静态的,所以我无法创建实例,但是当我尝试将 VB.NET 转换器转换为 C# 时,网站给了我这个:
Microsoft.Win32.Registry myreg = null;
并给我一条错误消息:'Microsoft.Win32.Registry' 是一种'类型',但用作'变量'
在 VB.NET 函数的最后一部分中,使用了 myreg:
Finally
myreg = Nothing
End Try
这是整个功能:
Imports System.Data.OleDb
Imports System.Data
Imports Microsoft.Win32.RegistryKey
Imports MyLogger 'custom reference, not worried about this.
Private Function getRegistryString(ByVal sVal As String, Optional ByVal sKey As String = "") As String
Dim myreg As Microsoft.Win32.Registry
Dim s As String
Try
s = myreg.LocalMachine.OpenSubKey(sKey).GetValue(sVal)
Catch ex As Exception
l.EventLog(ex.Message, EventLogEntryType.Error)
Throw ex
Finally
myreg = Nothing
End Try
Return s
s = Nothing
End Function