繁体   English   中英

在Visual Basic vb.NET中阅读INI

[英]Read INI in Visual Basic vb.NET

我正在尝试读取INI文件,然后将值转储到注册表中以使安装应用程序正常运行。

INI文件如下所示

[Setup Components]

Sybase Adaptive Server Anywhere Client=Yes

Borland Database Engine=Yes

BDERoot=c:\Program Files\Borland\BDE 

Program Shortcuts=Yes

ODBC Data Service Name=Yes

Local Client = Yes

Sybase Admin = Yes

Sybase Directory= C:\Program Files\SQL Anywhere 16

DBRoot=c:\Database

Word Link=Yes

Installation Root Folder=c:\program

FireDAC=DB=asa16;eng=ENGINE;dbn=DBNAME;links=TCPIP{Host=127.0.0.1;Port=2638}

[Program Shortcuts]

Desktop=Yes
Start Menu=Yes

Program Title=PROGRAM

EXE Pathname=c:\program.exe

Parameters=DBNAME

Starting Directory=c:\

Icon=icon.ICO


[ODBC Data Service Name]

Data Service Name=DBNAME

Database File Name=c:\database\database.db

Database Name=DBNAME

Server Name=ENGINE

Comm Links=TCPIP{}

PrefetchBuffer=6000

PrefetchRows=50

CommBufferSize=1460

Compress=no


[Service]

Service Name=SQLANYs_DBNAME
Display Name=SQL Anywhere - DBNAME
Service Group=SQLANYServer
Params=-n DBNAME -x TCPIP{} "C:\database\database.db" -n DBNAME

因此,我需要将所有这些项目轻松地转储到注册表中。

我正在使用Visual Studio 2015,但确实尝试使用Ludvik Jerabek的INI阅读器( http://www.codeproject.com/Articles/21896/INI-Reader-Writer-Class-for-C-VB-NET-and- VBScript ),但没有运气使它起作用!

我为上面使用的代码如下:

Dim ini = New IniFile()
ini.Load("setup.ini")
Dim readValue = ini.Sections("Service").Keys("Service Name")
MessageBox.Show(readValue.ToString)

运行此代码时,出现以下错误:“从字符串“ Service”转换为“ Integer”类型是无效的-此方法还意味着在INI文件中命名每个键,这将是一项艰巨的任务!

在阅读了此处的一些帮助问题后,我接着尝试了另一种方法,并使用了以下方法:

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String,
    ByVal lpKeyName As String,
    ByVal lpDefault As String,
    ByVal lpReturnedString As StringBuilder,
    ByVal nSize As Integer,
    ByVal lpFileName As String) As Integer


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sb As StringBuilder
    sb = New StringBuilder(500)
    Dim readVal = GetPrivateProfileString("Service", "Service Name", "", sb, sb.Capacity, "setup.ini")
    MessageBox.Show(readVal.ToString)
End Sub

但是,这仅返回“ 0”

任何帮助将不胜感激,找到一种从INI读取并转储到注册表的方法

使用给定的IniFile类,这将使您进入配置设置值:

Private Sub INIButtonTest_Click(sender As Object, e As EventArgs) Handles INIButtonTest.Click
    Try

        Dim iniFilePath As String = "H:\Dev\StackOverflow\StackOverflowTest\StackOverflowApp\bin\Debug\test.ini"

        Dim myIniFile As New IniFile
        myIniFile.Load(iniFilePath)

        Dim myValue As String = getIniValue(myIniFile, "Service", "Service Name")

        If Not String.IsNullOrEmpty(myValue) Then
            MessageBox.Show(String.Format("Found value: [{0}]", myValue))
        End If

    Catch ex As Exception
        MessageBox.Show(String.Concat("Something went wrong:", ex.Message))
    End Try
End Sub

Private Function getIniValue(iniFileInstance As IniFile, sectionName As String, sectionKey As String) As String

    Dim myValue As String = String.Empty

    For Each sect As IniFile.IniSection In iniFileInstance.Sections
        If sect.Name = sectionName Then
            For Each key As IniFile.IniSection.IniKey In sect.Keys
                If key.Name = sectionKey Then
                    myValue = key.GetValue
                    Exit For
                End If
            Next
            Exit For
        End If
    Next

    Return myValue

End Function

作为替代方案,原始方法的代码非常接近正确,但Kernel32.dll中实际上并不存在GetPrivateProfileString 您需要在名称上添加W,以使代码更正:

' With these imports
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Text

' Note the W on the function name
Private Declare Auto Function GetPrivateProfileStringW Lib "kernel32" (ByVal lpAppName As String,
    ByVal lpKeyName As String,
    ByVal lpDefault As String,
    ByVal lpReturnedString As StringBuilder,
    ByVal nSize As Integer,
    ByVal lpFileName As String) As Integer


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sb As New StringBuilder(500)
    Dim result As Integer = GetPrivateProfileStringW("Service", "Service Name", "", sb, sb.Capacity, "setup.ini")
    If result > 0 Then
        MessageBox.Show(sb.ToString())
    Else
        Dim ex As New Win32Exception(Marshal.GetLastWin32Error())
        MessageBox.Show(ex.Message)
    End If
End Sub

如果您不想使用W,或者您想对函数进行其他调用,则还可以使用DllImportAttribute并将函数声明更改为:

<DllImport("Kernel32.dll", CharSet:=CharSet.Auto, 
           SetLastError:=True, EntryPoint:="GetPrivateProfileStringW")>
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String,
                                                ByVal lpKeyName As String,
                                                ByVal lpDefault As String,
                                                ByVal lpReturnedString As StringBuilder,
                                                ByVal nSize As Integer,
                                                ByVal lpFileName As String) As Integer
End Function

这是我从VB 3.0开始使用的东西,并且从3.1开始就可以在所有Windows版本中使用。 进行了一些修改以使其适合较新的dev工具的约定,但本质上它们是相同的例程。

这些必须在公共班级或以下形式的公共区域中:

公共声明函数OSGetPrivateProfileString%Lib“ kernel32”别名“ GetPrivateProfileStringA”(ByVal AppName作为字符串,ByVal KeyName作为字符串,ByVal keydefault作为字符串,ByVal ReturnString作为字符串,ByVal NumBytes作为整数,ByVal FileName作为字符串)

公共声明函数OSWritePrivateProfileString Lib“ kernel32”别名“ WritePrivateProfileStringA”(ByVal AppName作为字符串,ByVal KeyName作为字符串,ByVal keydefault作为字符串,ByVal FileName作为字符串)作为整数

然后,在表单的代码区域中的某个地方,或者如果您有一个“ functions.vb”类(将在上面声明上述内容的地方),请放置以下两个:

公共共享函数GetINIString(ByVal strItem作为字符串,ByVal strDefault作为字符串,ByVal strSection作为字符串,ByVal文件名作为字符串)作为字符串

    Const BUFFERSIZE As Integer = 16768

    Dim strTemp As New String(Chr(0), BUFFERSIZE)
    Dim stringSize As Long = 0

    stringSize = OSGetPrivateProfileString%(strSection, strItem, strDefault, strTemp, BUFFERSIZE, filename)

    Return Strings.Left(strTemp, stringSize)

结束功能

公共共享子PutINIString(strItem作为字符串,strValue作为字符串,strSection作为字符串,iniFilename作为字符串)

Dim i As Integer = 0

i = OSWritePrivateProfileString(strSection, strItem, strValue, iniFilename)

结束子


这将继续在VB 2010中起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM