Recent Posts

Thursday, September 25, 2008

How to read or write ini file from visual basic .net Application?

Step 1

In a project add a module and add the following api declarations:

Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Int32

Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32


Step 2

Write the following functions to read or write ini files


Public Sub writeIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String)
Dim Result As Integer

Result = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)

End Sub

Public Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String

Dim ParamVal As String
Dim LenParamVal As Long

ParamVal = Space$(1024)
LenParamVal = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)

ReadIni = Left$(ParamVal, LenParamVal)


End Function


Step 3

Add a form and
a command button named cmdCheckIni
a label named lbldisplay
a text box named txtvalue

Now on the command button click event add the following:

Private Sub cmdCheckIni_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckIni.Click
Dim strFilepath As String
' create a file test.ini in the bin folder in your application path

strFilepath = My.Application.Info.DirectoryPath & "\test.ini"

lblDisplay.Text = ReadIni(strFilepath, "Logon", "LastUserId", "")
writeIni(strFilepath, "Logon", "LastUserId", txtValue.Text)
lblDisplay.Text = lblDisplay.Text & "!!!!" & ReadIni(strFilepath, "Logon", "LastUserId", "")

End Sub

your test.ini file should contain the following info
[Logon]
LastUserId=dd


Now you can run your application and test to see how it can store and retrieve the userID in the iniFile

This can be used both in Asp.net or Vb.net applications. The above code is written in visual basic 2005.

Related Posts by Categories




2 comments:

Anonymous said...

Hi.

Great example. Im using your code to save and load the checked property for checkboxes, the text property for textboxes, the selecteditem property for comboboxes and the backcolor property for panels.

Saving and loading these properties work great. I do however need to save and read one more thing:

The items in a Listbox.

Is that actually possible with your class and code? If so, could you give me an example?

Thanks. :)

Unknown said...

Good details read and write information in visual basic.

Convert VB to C#

VB6 to VB.Net Migration

Convert ASP to ASP.Net

Post a Comment