|
-
Apr 16th, 2008, 05:25 PM
#1
Thread Starter
Lively Member
[2008]Read/Write INI
I searched google,this forum & its FAQ & The help file. & all codes are hyper large & Ether there is no info(in hep file) or 100% of all codes are not working, even the ones that should write xml file, im getting syntax & declaration errors with absolutely all codes I found. I understand microsoft did this on purpose, so ppl would stop using ini files & go to xml based data storage...
Question: I want to store 2 values, see below: How on earth can I do it with possible small amount of code?
Code:
IniWrite("myfile.ini", "section1", "link1", "Completed")
IniRead("myfile.ini", "section1", "link1", "")
Last edited by goldenix; Apr 17th, 2008 at 07:34 PM.

M.V.B. 2008 Express Edition
-
Apr 16th, 2008, 05:41 PM
#2
Re: [2008]Read/Write INI
try this. use a filename for your own .ini file, or nothing for win.ini
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iFile As New IniFile(Nothing) 'My.Application.Info.DirectoryPath & "\test.ini")
''string
'iFile.WriteString("Settings", "ClockTime", "12:59")
'MsgBox(iFile.GetString("Settings", "ClockTime", "(none)"))
''integer
'iFile.WriteInteger("Settings", "test", 100)
'MsgBox(iFile.GetInteger("Settings", "test", 0))
''boolean
'iFile.WriteBoolean("Settings", "test", True)
'MsgBox(iFile.GetBoolean("Settings", "test", False))
End Sub
vb Code:
Public Class IniFile
Private strFilename As String
' Constructor, accepting a filename
Public Sub New(ByVal Filename As String)
strFilename = Filename
End Sub
' Read-only filename property
ReadOnly Property FileName() As String
Get
Return strFilename
End Get
End Property
Public Function GetString(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As String) As String
If FileName IsNot Nothing Then
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = API.GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then
Return Left(objResult.ToString, intCharCount)
Else
Return String.Empty
End If
Else
' Returns a string from your win.INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = API.GetProfileString(Section, Key, _
[Default], objResult, objResult.Capacity)
If intCharCount > 0 Then
Return Left(objResult.ToString, intCharCount)
Else
Return String.Empty
End If
End If
End Function
Public Function GetInteger(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Integer) As Integer
If FileName IsNot Nothing Then
' Returns an integer from your INI file
Return API.GetPrivateProfileInt(Section, Key, _
[Default], strFilename)
Else
' Returns an integer from your win.INI file
Return API.GetProfileInt(Section, Key, _
[Default])
End If
End Function
Public Function GetBoolean(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Boolean) As Boolean
If FileName IsNot Nothing Then
' Returns a boolean from your INI file
Return CBool(API.GetPrivateProfileInt(Section, Key, _
CBool([Default]), strFilename))
Else
' Returns a boolean from your win.INI file
Return CBool(API.GetProfileInt(Section, Key, _
CBool([Default])))
End If
End Function
Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
If FileName IsNot Nothing Then
' Writes a string to your INI file
API.WritePrivateProfileString(Section, Key, Value, strFilename)
Else
' Writes a string to your win.INI file
API.WriteProfileString(Section, Key, Value)
End If
Flush()
End Sub
Public Sub WriteInteger(ByVal Section As String, _
ByVal Key As String, ByVal Value As Integer)
' Writes an integer to your INI file
WriteString(Section, Key, CStr(Value))
End Sub
Public Sub WriteBoolean(ByVal Section As String, _
ByVal Key As String, ByVal Value As Boolean)
' Writes a boolean to your INI file
WriteString(Section, Key, CStr(CInt(Value)))
End Sub
Private Sub Flush()
' Stores all the cached changes to your INI file
API.FlushPrivateProfileString(0, 0, 0, strFilename)
End Sub
End Class
Public Class API
' API functions
Public Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer
Public 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
Public Declare Ansi Function GetPrivateProfileInt _
Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault As Integer, _
ByVal lpFileName As String) As Integer
Public Declare Ansi Function FlushPrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As Integer, _
ByVal lpKeyName As Integer, ByVal lpString As Integer, _
ByVal lpFileName As String) As Integer
Public Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" (ByVal lpszSection As String, _
ByVal lpszKeyName As String, ByVal lpszString As String) _
As Integer
Public Declare Function GetProfileString Lib "kernel32" Alias _
"GetProfileStringA" (ByVal lpAppName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer) As Integer
Public Declare Function GetProfileInt Lib "kernel32" _
Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal _
lpKeyName As String, ByVal nDefault As Integer) As Integer
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 16th, 2008, 06:06 PM
#3
Re: [2008]Read/Write INI
did that help?
just paste the class into your project + use it as shown in 1st code section
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 16th, 2008, 06:16 PM
#4
Thread Starter
Lively Member
Re: [2008]Read/Write INI
 Originally Posted by .paul.
did that help?
k i finished messing around with it &:
so much of the minimal amount of code... But at least its working code. Thanx i just made a new module & put those classes there (Out of my eyes.)
small correction to the integer sample, it must be declared or it will output test=-1:
Code:
'integer
Dim inter As Integer = 100
iFile.WriteInteger("Settings", "test", inter)
MsgBox(iFile.GetInteger("Settings", "test", inter))

M.V.B. 2008 Express Edition
-
Apr 16th, 2008, 06:43 PM
#5
Re: [RESOLVED][2008]Read/Write INI
i tried it again. i wrote it on my old PC in win xp.
on my new PC with 64bit vista, the win.ini stuff doesn't work but it still works with private ini files
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 16th, 2008, 06:50 PM
#6
Thread Starter
Lively Member
Re: [RESOLVED][2008]Read/Write INI
hopefully the xml file creation will be simpler in the future. for now ill stick with those ini files

M.V.B. 2008 Express Edition
-
Apr 17th, 2008, 07:27 PM
#7
Thread Starter
Lively Member
Re: [2008]Read/Write INI
 Originally Posted by .paul.
...
a Question. Any idea why isnt the ini file created, if I read filepath from registry? regread result is in the comment. I also tried to put the filepath i get from registry to clipboard & then read the filepath from clipboard & it didnt work eather. it does work if I use the My.Application.Info.DirectoryPath & "\test.ini" But my filepath isnt static, so i cant use that
Check this sample out
Code:
'Declaring redistry thing
'======================================
Dim objShell = CreateObject("WScript.Shell")
Dim regvalue = "HKEY_CURRENT_USER\Software\_MyScripts\Getlinks"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'C:\Documents and Settings\user\Desktop\wordpress.com.txt
Dim iFile As New IniFile(objShell.RegRead(regvalue & "\last_saved_links_to"))
Dim link As String = "test"
iFile.WriteString("section1", link, "Completed")
End Sub

M.V.B. 2008 Express Edition
-
Jun 10th, 2008, 09:42 PM
#8
Hyperactive Member
Re: [2008]Read/Write INI
really hate to bring an old topic to life but i think you can do application.startuppath & "\ininame.ini" that way where ever the exe is is where the ini will be created or you can use a save dialog box and get the file path from there.
-
Jun 23rd, 2008, 11:18 PM
#9
Member
Re: [2008]Read/Write INI
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iFile As New IniFile(Nothing) 'My.Application.Info.DirectoryPath & "\test.ini")
''string
'iFile.WriteString("Settings", "ClockTime", "12:59")
'MsgBox(iFile.GetString("Settings", "ClockTime", "(none)"))
''integer
'iFile.WriteInteger("Settings", "test", 100)
'MsgBox(iFile.GetInteger("Settings", "test", 0))
''boolean
'iFile.WriteBoolean("Settings", "test", True)
'MsgBox(iFile.GetBoolean("Settings", "test", False))
End Sub
Code:
Public Class IniFile
Private strFilename As String
' Constructor, accepting a filename
Public Sub New(ByVal Filename As String)
strFilename = Filename
End Sub
' Read-only filename property
ReadOnly Property FileName() As String
Get
Return strFilename
End Get
End Property
Public Function GetString(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As String) As String
If FileName IsNot Nothing Then
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = API.GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then
Return Left(objResult.ToString, intCharCount)
Else
Return String.Empty
End If
Else
' Returns a string from your win.INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = API.GetProfileString(Section, Key, _
[Default], objResult, objResult.Capacity)
If intCharCount > 0 Then
Return Left(objResult.ToString, intCharCount)
Else
Return String.Empty
End If
End If
End Function
Public Function GetInteger(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Integer) As Integer
If FileName IsNot Nothing Then
' Returns an integer from your INI file
Return API.GetPrivateProfileInt(Section, Key, _
[Default], strFilename)
Else
' Returns an integer from your win.INI file
Return API.GetProfileInt(Section, Key, _
[Default])
End If
End Function
Public Function GetBoolean(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Boolean) As Boolean
If FileName IsNot Nothing Then
' Returns a boolean from your INI file
Return CBool(API.GetPrivateProfileInt(Section, Key, _
CBool([Default]), strFilename))
Else
' Returns a boolean from your win.INI file
Return CBool(API.GetProfileInt(Section, Key, _
CBool([Default])))
End If
End Function
Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
If FileName IsNot Nothing Then
' Writes a string to your INI file
API.WritePrivateProfileString(Section, Key, Value, strFilename)
Else
' Writes a string to your win.INI file
API.WriteProfileString(Section, Key, Value)
End If
Flush()
End Sub
Public Sub WriteInteger(ByVal Section As String, _
ByVal Key As String, ByVal Value As Integer)
' Writes an integer to your INI file
WriteString(Section, Key, CStr(Value))
End Sub
Public Sub WriteBoolean(ByVal Section As String, _
ByVal Key As String, ByVal Value As Boolean)
' Writes a boolean to your INI file
WriteString(Section, Key, CStr(CInt(Value)))
End Sub
Private Sub Flush()
' Stores all the cached changes to your INI file
API.FlushPrivateProfileString(0, 0, 0, strFilename)
End Sub
End Class
Public Class API
' API functions
Public Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer
Public 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
Public Declare Ansi Function GetPrivateProfileInt _
Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault As Integer, _
ByVal lpFileName As String) As Integer
Public Declare Ansi Function FlushPrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As Integer, _
ByVal lpKeyName As Integer, ByVal lpString As Integer, _
ByVal lpFileName As String) As Integer
Public Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" (ByVal lpszSection As String, _
ByVal lpszKeyName As String, ByVal lpszString As String) _
As Integer
Public Declare Function GetProfileString Lib "kernel32" Alias _
"GetProfileStringA" (ByVal lpAppName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer) As Integer
Public Declare Function GetProfileInt Lib "kernel32" _
Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal _
lpKeyName As String, ByVal nDefault As Integer) As Integer
End Class
REposting becouse i cant use the vb code tags it alsways copies the number.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|