vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Saturday, April 16, 2011

How to Read And Write .INI files in vb.net?


Hi..
when i am doing a project i need an requirement to read and write the INI files.then i googled ,But i got only partly result..
lastly i found this solution to read INI files ,then i decide to write articles to read INI files...

First you have to declare a function to read and write INI files..
Private Declare Ansi Function GetPrivateProfileString  Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
        (ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, ByVal lpDefault As String, _
        ByVal lpReturnedString As String, _
        ByVal nSize As Integer, ByVal lpFileName As String) _
        As Integer
    Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
           (ByVal lpApplicationName As String, _
           ByVal lpKeyName As String, ByVal lpString As String, _
           ByVal lpFileName As String) As Integer

Details About Declaration:

Function GetPrivateProfileString:

lpkeyname -->Denotes session of INI file
 lpDefault---->Its return default value
lpReturnedString--->its gets the value from the INI file
nSize---->Declare a size for return string
lpFileName-->Its path of INI file

Function WritePrivateProfileString
lpApplicationName--->Session Name
lpKeyName---->
lpString------>Input text
lpFilename---->Its path of INI file

In form add two buttons one for Read INI files and another one to write.......

In an Button click event enter this code:


    Private Sub Read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Read.Click
textbo1.text=Getinistring("Boys1","students")
End Sub


 Public Function Getinistring(ByVal strKey As String, _
Optional ByVal strSection As String ) As String
        Dim strValue As String
        Dim intPos As Integer
            strValue = Space(1024)
        GetPrivateProfileString(strSection, strKey, "Not-Found", strValue, 1024, "D:\class.ini")
           strValue = Mid(strValue, 1, Len(strValue) - 1)
           strValue = Trim(Left(strValue, Len(Trim(strValue)) - 1))
        
        Getinistring = strValue


    End Function
  Private Sub Write_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  Write.Click
Dim rec as long
rec=WritePrivateProfileString("students", "Boys1", Trim(textbo2.Text), "D:\class.ini")
End Sub

 Sample INI File

i hope this really helpful to you plz post your valuable comments...............

2 comments:

Garrox said...

hope you still get notifies on this article..

i ALWAYS wrote stuff for registry (very easier and nicer imo). but now i wrote an app for someone with a win7 machine. the only thing he gets exception on is the registry write code (reading is no problem). so i read on inet it has something todo with win7 security. i changed the regpath from localmachine to current user ("software/myapp"). not working.. added permissioncheck and elevation code... not working. i now dont want to write a fix anymore, cuz i feel it will be an unstable fix IF i find one. sooo... now going to digg ini files.. wich i never done before.
ur article is THE most usefull article on this subject. since it will be NOT my own idea im gonna write. i specifically searched for 2 pieces of code (read and write), and some line 2 call the functions. exactly what i found in ur article. way to go man. not for the special code.. but for being the only one (wich i can find) with usefull/helpfull/allcovering info.
THNX. grz. Tommy

vijay said...

Thanks...

Post a Comment