Advertisement
julioCCs

read and write .ini files

Dec 2nd, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.70 KB | None | 0 0
  1. Private iniFile As String
  2.  
  3. Private Declare Auto Function GetPrivateProfileString Lib "Kernel32" _
  4.         (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
  5.  
  6.     Private Declare Auto Function WritePrivateProfileString Lib "Kernel32" _
  7.         (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
  8.  
  9.     Private Function ReadINI(ByVal key_name As String, ByVal section_name As String, ByVal default_value As String) As String
  10.         Const MAX_LENGTH As Integer = 500
  11.         Dim string_builder As New StringBuilder(MAX_LENGTH)
  12.  
  13.         GetPrivateProfileString(section_name, key_name, default_value, string_builder, MAX_LENGTH, iniFile)
  14.  
  15.         Return string_builder.ToString()
  16.     End Function
  17.  
  18.     Private Sub WriteINI(ByVal key_name As String, ByVal section_name As String, ByVal value As String)
  19.         WritePrivateProfileString(section_name, key_name, value, iniFile)
  20.     End Sub
  21.  
  22.     Private Sub checkIniExists()
  23.         If Not File.Exists(iniFile) Then
  24.             File.CreateText(iniFile).Dispose()
  25.  
  26.             Wait(100)
  27.         End If
  28.     End Sub
  29.  
  30.     Private Function Read_INI_String(opt As String, categoy As String, def As String) As String
  31.         If ReadINI(opt, categoy, "") = "" Then WriteINI(opt, categoy, def)
  32.  
  33.         Return ReadINI(opt, categoy, def)
  34.     End Function
  35.  
  36.  
  37. 'set the path in iniFile variable
  38. 'call checkIniExists to make sure that the .ini file exists
  39. 'use ReadIni and WriteIni
  40. 'use Read_INI_String when you want set a default value when there is no value in the variable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement