home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD46834112000.psc / Mod_Ini.bas next >
Encoding:
BASIC Source File  |  2000-03-31  |  6.4 KB  |  166 lines

  1. Attribute VB_Name = "Module1"
  2. 'INI Read and Write
  3. Declare Function GetPrivateProfileString Lib "kernel32" _
  4.     Alias "GetPrivateProfileStringA" (ByVal lpApplicationName _
  5.     As String, lpKeyName As Any, ByVal lpDefault As String, _
  6.     ByVal lpRetunedString As String, ByVal nSize As Long, _
  7.     ByVal lpFilename As String) As Long
  8.  
  9.  
  10. Declare Function WritePrivateProfileString Lib "kernel32" _
  11.     Alias "WritePrivateProfileStringA" (ByVal lpApplicationName _
  12.     As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
  13.     ByVal lplFileName As String) As Long
  14. 'INI Read and Write
  15.                         
  16. Public Function INIRead(iAppName As String, iKeyName As String, iFileName As String) As String
  17.     'Example:
  18.     'x = INIRead("boot", "shell", "C:\WINDOWS\system.ini")
  19.     ' ou boot est la section, shell la cle
  20.     
  21.     Dim iStr As String
  22.     
  23.     iStr = String(255, Chr(0))
  24.     INIRead = Left(iStr, GetPrivateProfileString(iAppName, ByVal iKeyName, "", iStr, Len(iStr), iFileName))
  25.     
  26. End Function
  27.  
  28. Public Function INIWrite(iAppName As String, iKeyName As String, iKeyString As String, iFileName As String)
  29.     'Example:
  30.     'x = INIWrite("boot", "shell", "Explorer.exe", "C:\WINDOWS\system.ini")
  31.     ' ou boot est la section, shell la cle
  32.     
  33.     r% = WritePrivateProfileString(iAppName, iKeyName, iKeyString, iFileName)
  34.     
  35. End Function
  36.  
  37.  
  38. Public Function GetKeyVal(ByVal Section As String, ByVal Key As String, ByVal INIFileLoc As String)
  39.     'This Function retrieves information fro
  40.     '     m an INI File
  41.     'INIFileLoc = The location of the INI Fi
  42.     '     le (ex. "C:\Windows\INIFile.ini")
  43.     'Section = Section where the Key is held
  44.     '
  45.     'Key = The Key of which you want to retr
  46.     '     ieve information
  47.     'Checking to see if the INI File specifi
  48.     '     ed exists
  49.     If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'GetKeyVal'", vbExclamation, "INI File Not Found": Exit Function
  50.     'If INI File exists then proceed to Get
  51.     '     Key Value
  52.     Dim RetVal As String, Worked As Integer
  53.     RetVal = String$(255, 0)
  54.     Worked = GetPrivateProfileString(Section, Key, "", RetVal, Len(RetVal), INIFileLoc)
  55.  
  56.  
  57.     If Worked = 0 Then
  58.         GetINI = ""
  59.     Else
  60.         GetINI = Left(RetVal, InStr(RetVal, Chr(0)) - 1)
  61.     End If
  62. End Function
  63.  
  64.  
  65. Function AddToINI(ByVal Section As String, ByVal Key As String, ByVal Value As String, ByVal INIFileLoc As String)
  66.     'This Function adds a Section, Key, or V
  67.     '     alue to an INI file
  68.     'Also used to CREATE NEW INI FILE
  69.     'INIFileLoc = The location of the INI Fi
  70.     '     le (ex. "C:\Windows\INIFile.ini")
  71.     'Section = The name of the referred to S
  72.     '     ection or newly created Section (ex. "Ne
  73.     '     w Section 1")
  74.     'Key = The name of the referred to Key o
  75.     '     r newly created Key (ex. "New Key 1")
  76.     'Value = The value to hold in the given
  77.     '     Key (ex. "New Info Held")
  78.     'Checking to see if the INI File specifi
  79.     '     ed exists
  80.     If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'AddToINI'", vbExclamation, "INI File Not Found": Exit Function
  81.     'If INI File exists then proceed to Add
  82.     '     the information to the INI File
  83.     WritePrivateProfileString Section, Key, Value, INIFileLoc
  84. End Function
  85.  
  86.  
  87. Public Function DeleteSection(ByVal Section As String, ByVal INIFileLoc As String)
  88.     'This Function Deletes a specified Secti
  89.     '     on from an INI file
  90.     'INIFileLoc = The location of the INI Fi
  91.     '     le (ex. "C:\Windows\INIFile.ini")
  92.     'Section = The name of the Section you w
  93.     '     ish to remove (ex. "Section Number 1")
  94.     'Checking to see if the INI File specifi
  95.     '     ed exists
  96.     If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteSection'", vbExclamation, "INI File Not Found": Exit Function
  97.     'If INI File exists then proceed to dele
  98.     '     te Section
  99.     WritePrivateProfileString Section, vbNullString, vbNullString, INIFileLoc
  100.     'NOTE: vbNullString is the coding in whi
  101.     '     ch to delete a Section, or Key
  102. End Function
  103.  
  104.  
  105. Public Function DeleteKey(ByVal Section As String, ByVal Key As String, ByVal INIFileLoc As String)
  106.     'This Function Deletes a Key in a specif
  107.     '     ied Section from an INI file
  108.     'INIFileLoc = The location of the INI Fi
  109.     '     le (ex. "C:\Windows\INIFile.ini")
  110.     'Section = The name of the Section in wh
  111.     '     ich the Key to be deleted is held (ex. "
  112.     '     Section Number 1")
  113.     'Key = The name of the Key you wish to r
  114.     '     emove (ex. "Key Number 5")
  115.     'Checking to see if the INI File specifi
  116.     '     ed exists
  117.     If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteKey'", vbExclamation, "INI File Not Found": Exit Function
  118.     'If INI File exists then proceed to dele
  119.     '     te Key
  120.     WritePrivateProfileString Section, Key, vbNullString, INIFileLoc
  121.     'NOTE: vbNullString is the coding in whi
  122.     '     ch to delete a Section, or Key
  123. End Function
  124.  
  125.  
  126. Public Function DeleteKeyValue(ByVal Section As String, ByVal Key As String, ByVal INIFileLoc As String)
  127.     'This Function deletes the value in a sp
  128.     '     ecified Key from an INI file
  129.     'INIFileLoc = The location of the INI Fi
  130.     '     le (ex. "C:\Windows\INIFile.ini")
  131.     'Section = The name of the Section in wh
  132.     '     ich the Key is held (ex. "Section Number
  133.     '     1")
  134.     'Key = The name of the Key you wish to r
  135.     '     emove the value from (ex. "Key Number 5"
  136.     '     )
  137.     'Checking to see if the INI File specifi
  138.     '     ed exists
  139.     If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteKeyValue'", vbExclamation, "INI File Not Found": Exit Function
  140.     'If INI File exists then proceed to dele
  141.     '     te Key Value
  142.     WritePrivateProfileString Section, Key, "", INIFileLoc
  143.     ' "" = is a short way of saying Nothing
  144. End Function
  145.  
  146.  
  147. Public Function RenameSection()
  148.     'Coming Soon
  149. End Function
  150.  
  151.  
  152. Public Function RenameKey()
  153.     'Coming Soon
  154. End Function
  155.  
  156.  
  157. Public Sub WhiteLine(iFileName As String)
  158. 'insert a white in the ini file
  159.     Open iFileName For Append Access Read Write As #1
  160.     Print #1, " "
  161.     Close #1
  162.         
  163. End Sub
  164.  
  165.  
  166.