[英]Open INI file and place content in different textboxes
I am creating a program in VB.net that opens a .INI file and reads the content. 我正在VB.net中创建一个程序,该程序打开.INI文件并读取内容。 I wrote the following code: 我写了以下代码:
Private Sub OpenINIButton_Click(sender As Object, e As EventArgs) Handles OpenINIButton.Click
Dim OpenDLG As New OpenFileDialog
Dim FileLocation = OpenDLG.FileName.ToString()
OpenDLG.Filter = "INI File (*.ini)|*.ini"
OpenDLG.Title = "Open INI File"
OpenDLG.InitialDirectory = "C:\"
OpenDLG.RestoreDirectory = True
DialogResult = OpenDLG.ShowDialog
If DialogResult = Windows.Forms.DialogResult.OK Then
TextBox1.Text = ReadIni(FileLocation, INIkey, INIvalue, "")
ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If
End Sub
The open file dialog opens and I can open a INI file but the value of INIkey is not placed in TextBox1. 打开文件对话框打开,我可以打开一个INI文件,但INIkey的值未放置在TextBox1中。
Any idea how I can fix this? 知道我该如何解决吗?
You initialize the path
beore the user has selected it: 您初始化path
beore用户已选择它:
Dim FileLocation = OpenDLG.FileName.ToString()
Do that after ShowDialog
, so here is the correct place: 在ShowDialog
之后执行此操作,因此这里是正确的位置:
If DialogResult = Windows.Forms.DialogResult.OK Then
Dim FileLocation = OpenDLG.FileName.ToString()
TextBox1.Text = ReadIni(FileLocation, INIkey, INIvalue, "")
ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If
But why don't you notice an exception? 但是,为什么不注意到异常呢? I assume that you have an empty catch block where you try to open the file in ReadIni
. 我假设您有一个空的catch块,您尝试在其中打开ReadIni
的文件。 That's a bad habit to kick anyway. 反正这是个坏习惯。
Note that the FileLocation
variable in your code references the String
OpenDLG.FileName
at the time you assign it, there is no permanent connection between both. 请注意,代码中的FileLocation
变量在分配时引用了String
OpenDLG.FileName
,两者之间没有永久连接。 So if it changes later the variable references still the old string. 因此,如果以后更改,变量仍将引用旧字符串。
The best way to do this would be to make an iniParse module like below and just use the function as shown: 最好的方法是制作一个如下所示的iniParse模块,并仅使用如下所示的函数:
Module iniParse
Public readwrtie As Integer
Public settingValueReturn As New System.Text.StringBuilder(255)
Private Declare Auto Function WritePrivateProfileString Lib "Kernel32" _
(ByVal IpApplication As String, ByVal Ipkeyname As String, _
ByVal IpString As String, ByVal IpFileName As String) As Integer
Private Declare Auto Function GetPrivateProfileString Lib "Kernel32" _
(ByVal IpApplicationName As String, ByVal IpKeyName As String, _
ByVal IpDefault As String, ByVal IPReturnedString As System.Text.StringBuilder, _
ByVal nsize As Integer, ByVal IpFileName As String) As Integer
Public Sub WriteINIFile(heading As String, setting As String, settingvalue As String, path As String)
WritePrivateProfileString(heading, setting, settingvalue, path)
End Sub
Public Sub ReadIniFile(heading As String, setting As String, path As String)
GetPrivateProfileString(heading, setting, "", settingValueReturn, 100, path)
End Sub
End Module
Example: 例:
Button1_click blah blah blah handles button1.click...
ReadIniFile("MAIN", "test", "C:\config.ini")
'this would read the following ini file:
'[MAIN]
'test=hi
'to get that 'hi' value you would use this code:
textbox1.text = settingReturnValue.tostring '(settingValueReturn Will always be the value of the setting entered in the function args)
'to write ini file:
Button1_click blah blah blah handles button1.click...
WriteIniFile("MAIN", "test2", "hi2", "C:\config.ini")
'this would write the following to the ini @ C:\config.ini file:
'[MAIN]
'test2=hi2
I hope this helps your needs! 希望这可以满足您的需求!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.