[英]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.