Subscribe:Posts Comments

You Are Here: Home � � Read and Write INI file

This sample code how to create visual basic program to write and read ini file :

VERSION 5.00
Begin VB.Form frmReadAndWriteIni
Caption = "Form1"
ClientHeight = 2265
ClientLeft = 60
ClientTop = 345
ClientWidth = 4590
LinkTopic = "Form1"
LockControls = -1 'True
ScaleHeight = 2265
ScaleWidth = 4590
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdWritePrivateProfileSetting
Caption = "&Write Private Profile Setting"
Height = 840
Left = 150
TabIndex = 1
Top = 1200
Width = 4125
End
Begin VB.CommandButton cmdReadFromIni
Caption = "&Read From Ini"
Height = 840
Left = 150
TabIndex = 0
Top = 150
Width = 4125
End
End
Attribute VB_Name = "frmReadAndWriteIni"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1999-2005, Vivek Nigam All Rights Reserved.
' Code contain copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' #VBCode#************************************************************
' * Programmer Name : Vivek Nigam
' * Web Site :
' * E-Mail : vivek.nigam19@hotmail.com
' * Date : 16/04/2002
' * Time : 12:40 PM
' * Module Name : Read And Write your Ini Files
' * Module Filename : Prjini.vbp
' * Procedure Name :
' * Parameters :
' *
' **********************************************************************
' * Comments : Read And write From Ini
' *
' **********************************************************************
Dim RtnStr As String ' receives the value read from the INI file
Dim sRetVal As Long ' receives length of the returned string
Dim ComputerName As String
Dim UserName As String
Dim ScanDate As Variant
Dim OperatingSystem As String
Dim ServicePack As String
Dim WindowsVersion As String
Dim CPUManufacturer As String
Dim CPUType As String
Dim CPUSpeed As Integer
Dim MemoryTotal As Long
Dim MemoryPaged As Long
Dim FixedDrive As String
Dim CdRom As String
Dim NetworkDrive As String
Dim SourceFolder As String
Dim objFSO As Object
Dim ObjFile As Variant
Dim ObjFolder As Variant
Dim ProductName As String
Dim ProductVersion As String
Dim PrinterName As String
Dim PrinterConnection As String
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Sub ReadFromINI()
'---------------------------------------------------------------------------------
'Procedure: ReadFromINI
'Parameter:
'Return Value :RtnStr : Number Of Characters Read From The Files
'Description : This function call the WindowsAPI Function (GetPrivateProfileString) To read data from the section from INI
'---------------------------------------------------------------------------------
On Error Resume Next
sRetVal = GetPrivateProfileString("PCInfo", "ComputerName", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
ComputerName = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "UserName", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
UserName = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "ScanDate", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
ScanDate = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "OperatingSystem", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
OperatingSystem = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "ServicePack", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
ServicePack = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "WindowsVersion", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
WindowsVersion = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "CPUManufacturer", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
CPUManufacturer = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "CPUType", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
CPUType = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "CPUSpeed", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
CPUSpeed = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "MemoryTotal", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
MemoryTotal = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("PCInfo", "MemoryPaged", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
MemoryPaged = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("Drives", "Fixed Drive", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
FixedDrive = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("Drives", "Cd-Rom", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
CdRom = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

sRetVal = GetPrivateProfileString("Drives", "Network Drive", "0", RtnStr, 255, SourceFolder & "\" & ObjFile.Name)
NetworkDrive = Left$(RtnStr, sRetVal) ' extract the returned string from the buffer

Call ReadPrinter
Call ReadSoftware

End Sub
Sub ReadPrinter()
On Error Resume Next
Dim TextLine As String
Dim strTemp1 As String
Dim strTemp2 As String
Dim strTemp3 As String
Dim strTemp4 As String
Dim BlankLnFlag As Boolean
BlankLnFlag = False
Open SourceFolder & "\" & ObjFile.Name For Input As #1 ' Open file.
strTemp1 = "PrinterName="
strTemp2 = "PrinterConnection="
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine
strTemp3 = Left(TextLine, 12)
If TextLine = "" And BlankLnFlag = True Then
Exit Do
End If
If strTemp1 = strTemp3 Then
BlankLnFlag = True
PrinterName = Mid(TextLine, Len(strTemp1) + 1, Len(Trim(TextLine)))
If Not EOF(1) Then
Line Input #1, TextLine
strTemp4 = Left(TextLine, 18)
If strTemp2 = strTemp4 Then
PrinterConnection = Mid(TextLine, Len(strTemp4) + 1, Len(Trim(TextLine)))
End If
End If
End If
Loop
Close #1 ' Close file
End Sub
Sub ReadSoftware()
On Error Resume Next
Dim TextLine As String
Dim strTemp1 As String
Dim strTemp2 As String
Dim strTemp3 As String
Dim strTemp4 As String

Open SourceFolder & "\" & ObjFile.Name For Input As #1 ' Open file.
strTemp1 = "ProductName="
strTemp2 = "ProductVersion="
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine
strTemp3 = Left(TextLine, 12)
If strTemp1 = strTemp3 Then
ProductName = Mid(TextLine, Len(strTemp1) + 1, Len(Trim(TextLine)))
If Not EOF(1) Then
Line Input #1, TextLine
strTemp4 = Left(TextLine, 15)
If strTemp2 = strTemp4 Then
ProductVersion = Mid(TextLine, Len(strTemp4) + 1, Len(Trim(TextLine)))
End If
End If
End If
Loop
Close #1 ' Close file
End Sub
Sub ReadFiles()
On Error GoTo ErrorHandler ' Enable error-handling routine.
Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceFolder = App.Path
If objFSO.FolderExists(SourceFolder) Then
Set ObjFolder = objFSO.GetFolder(SourceFolder)
For Each ObjFile In ObjFolder.Files
'If InStr(ObjFile.Type, UCase("INI")) > 0 Then '// For reading the Type of File
If InStr(ObjFile, ".ini") > 0 Then
Call ReadFromINI '// Ok
'Kill (SourceFolder & "/" & ObjFile.Name)'// Please remove the comments if you wants to kill file after reading
End If
Next
End If
Set ObjFolder = Nothing
Set ObjFile = Nothing
Set objFSO = Nothing
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
Set ObjFolder = Nothing
Set ObjFile = Nothing
Set objFSO = Nothing
End
End Sub

Private Sub cmdReadFromIni_Click()
RtnStr = Space(255) ' provide enough room for the function to put the value into the buffer
Call ReadFiles
Debug.Print ComputerName & ";" & UserName & ";" & ScanDate & ";" & OperatingSystem & ";" & ServicePack _
& ";" & WindowsVersion & ";" & CPUManufacturer & ";" & CPUType & ";" & CPUSpeed _
& ";" & MemoryTotal & ";" & MemoryPaged & ";" & FixedDrive & ";" & CdRom _
& ";" & NetworkDrive & ";" & PrinterName & ";" & PrinterConnection & ";" & ProductName _
& ";" & ProductVersion

End Sub
Private Sub cmdWritePrivateProfileSetting_Click()
Call WriteToINI
End Sub

Sub WriteToINI()
'---------------------------------------------------------------------------------
'Procedure: WriteToINI
'Parameter:
'Return Value :RtnStr : Number Of Characters Read From The Files
'Description : This function call the WindowsAPI Function (WritePrivateProfileString) To store data into INI
'sRetVal = WritePrivateProfileString(SectionName, KeyField, KeyVal, FilePath)
'---------------------------------------------------------------------------------
sRetVal = WritePrivateProfileString("Personal Information", "Name", "Vivek Nigam", App.Path & "\WriteIniFile.ini")
sRetVal = WritePrivateProfileString("Personal Information", "Address", "UK", App.Path & "\WriteIniFile.ini")
sRetVal = WritePrivateProfileString("EmailAddress", "EmailId", "vivek_nigam19@hotmail.com", App.Path & "\WriteIniFile.ini")
End Sub
Tags:

0 comments

Leave a Reply