[Help] WriteIniValue [Solved]

Posts 1–15 of 17 · Page 1 of 2
[Help] WriteIniValue [Solved]
I have searched for hours after how to read and write ini values.
I know it can be done because I found a working function/module before my pc crashed, but now I can find it. I have found how to do it in vb6 but I want it to work on visual basic 2008

Dont say "Google it" because I have googled for almost 4 hours without success.

Thanks

-hej
Its easy, which ini file you want to read / write ??? Is it in a particular order... ?

Let me know these two things, and I will write the code. It will be short !!
Quote Originally Posted by MJLover_2 View Post
Its easy, which ini file you want to read / write ??? Is it in a particular order... ?

Let me know these two things, and I will write the code. It will be short !!
I want to create a new .ini file for example:
[php][Path]
Num1:
Num2:[/php]
Then I also want to read it.
OK:

To write an ini file use the following:


Code:
Sub WriteINI()
Dim file As String = "C:\hejsan.ini"
        Dim content As String = "[Path]='Enter path here...'" & vbCrLf & "Num1: 60" & vbCrLf & "Num2: 80" & vbCrLf & "blah blah blah..."

        My.Computer.FileSystem.WriteAllText(file, content, False)
End Sub
To read ini use:

Code:
Sub ReadINI()
Dim file As String = "C:\hejsan.ini"
Dim read As String = My.Computer.FileSystem.ReadAllText(file)
        MsgBox(read)
End Sub()
Then just call these subs where appropriate !!

Hope this helps, and I assume that this is what you wanna achieve !!
Quote Originally Posted by MJLover_2 View Post
OK:

To write an ini file use the following:


Code:
Sub WriteINI()
Dim file As String = "C:\hejsan.ini"
        Dim content As String = "[Path]='Enter path here...'" & vbCrLf & "Num1: 60" & vbCrLf & "Num2: 80" & vbCrLf & "blah blah blah..."

        My.Computer.FileSystem.WriteAllText(file, content, False)
End Sub
To read ini use:

Code:
Sub ReadINI()
Dim file As String = "C:\hejsan.ini"
Dim read As String = My.Computer.FileSystem.ReadAllText(file)
        MsgBox(read)
End Sub()
Then just call these subs where appropriate !!

Hope this helps, and I assume that this is what you wanna achieve !!
Sorry but that is not how I want it.
[php][Path]
Num1: File
Num2: File2[/php]

I want to read and write to num1 and num2.
You need a bit more advanced code but thanks anyways
Quote Originally Posted by Zoom View Post


Sorry but that is not how I want it.
[php][Path]
Num1: File
Num2: File2[/php]

I want to read and write to num1 and num2.
You need a bit more advanced code but thanks anyways
Still Easy

Code:
Sub INI()


        Dim file As String = "C:\hejsan.ini"
        Dim x As New IO.StreamReader(file)
        Dim finalstring As String = ""
        While Not x.EndOfStream

            Dim temp As String = x.ReadLine

            If temp.StartsWith("Num1: ") Then
                'First remove the 'Num1: ' from the line
                temp = temp.Replace("Num1: ", "")

                'The following lines will replace the contents of the line that starts with Num1

                Dim replacement As String = "xxx"
                temp = temp.Replace("File", replacement)

                'Rewrite 'Num1: '
                finalstring += "Num1: " & temp

            ElseIf temp.StartsWith("Num2: ") Then

                'First remove the 'Num2: ' from the line
                temp = temp.Replace("Num2: ", "")

                'The following lines will replace the contents of the line that starts with Num2

                Dim replacement As String = "LOL"
                temp = temp.Replace("File2", replacement)

                'Rewrite 'Num2: '
                finalstring += "Num2: " & temp
            Else
                'Write the lines which don't need editing...
                finalstring += temp

            End If


        End While

        'Now just write the finalstring variable to INI file...
        My.Computer.FileSystem.WriteAllText(file, finalstring, False)


    End Sub
Let me know if this is what you wanna do !!!
Sec putting my code up here which I actually used for my cod4 server tool.

Create a module(reading .ini)

Code:
Module Module1
    Public Function ReadIniValue(ByRef INIpath As String, ByRef KEY As String, ByRef Variable As String) As String
        Dim NF As Short
        Dim Temp As String
        Dim LcaseTemp As String
        Dim ReadyToRead As Boolean

AssignVariables:
        NF = FreeFile
        ReadIniValue = ""
        KEY = "[" & LCase(KEY) & "]"
        Variable = LCase(Variable)

EnsureFileExists:
        FileOpen(NF, INIpath, OpenMode.Binary)
        FileClose(NF)
        SetAttr(INIpath, FileAttribute.Archive)

LoadFile:
        FileOpen(NF, INIpath, OpenMode.Input)
        While Not EOF(NF)
            Temp = LineInput(NF)
            LcaseTemp = LCase(Temp)
            If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
            If LcaseTemp = KEY Then ReadyToRead = True
            If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
                If InStr(LcaseTemp, Variable & "=") = 1 Then
                    ReadIniValue = Mid(Temp, 1 + Len(Variable & "="))
                    FileClose(NF) : Exit Function
                End If
            End If
        End While
        FileClose(NF)
    End Function

End Module
Declare a .ini file:

Code:
Dim objIniFile As New IniFile(Application.StartupPath & "\data.ini")
Writing into the .ini file:

Code:
objIniFile.WriteString("Settings", "Whatever", Textbox1.Text)
        objIniFile.WriteString("Settings", "I need to pee", Textbox2.Text)
Reading from the .ini file:

Code:
Textbox1.Text.Text = ReadIniValue(Application.StartupPath & "\data.ini", "Settings", "Whatever")
        Textbox2.Text = ReadIniValue(Application.StartupPath & "\data.ini", "Settings", "I need to pee")
Quote Originally Posted by Blubb1337 View Post
Sec putting my code up here which I actually used for my cod4 server tool.

Create a module(reading .ini)

Code:
Module Module1
    Public Function ReadIniValue(ByRef INIpath As String, ByRef KEY As String, ByRef Variable As String) As String
        Dim NF As Short
        Dim Temp As String
        Dim LcaseTemp As String
        Dim ReadyToRead As Boolean

AssignVariables:
        NF = FreeFile
        ReadIniValue = ""
        KEY = "[" & LCase(KEY) & "]"
        Variable = LCase(Variable)

EnsureFileExists:
        FileOpen(NF, INIpath, OpenMode.Binary)
        FileClose(NF)
        SetAttr(INIpath, FileAttribute.Archive)

LoadFile:
        FileOpen(NF, INIpath, OpenMode.Input)
        While Not EOF(NF)
            Temp = LineInput(NF)
            LcaseTemp = LCase(Temp)
            If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
            If LcaseTemp = KEY Then ReadyToRead = True
            If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
                If InStr(LcaseTemp, Variable & "=") = 1 Then
                    ReadIniValue = Mid(Temp, 1 + Len(Variable & "="))
                    FileClose(NF) : Exit Function
                End If
            End If
        End While
        FileClose(NF)
    End Function

End Module
Declare a .ini file:

Code:
Dim objIniFile As New IniFile(Application.StartupPath & "\data.ini")
Writing into the .ini file:

Code:
objIniFile.WriteString("Settings", "Whatever", Textbox1.Text)
        objIniFile.WriteString("Settings", "I need to pee", Textbox2.Text)
Reading from the .ini file:

Code:
Textbox1.Text.Text = ReadIniValue(Application.StartupPath & "\data.ini", "Settings", "Whatever")
        Textbox2.Text = ReadIniValue(Application.StartupPath & "\data.ini", "Settings", "I need to pee")
Cool, but not really Cool !!!
Quote Originally Posted by Blubb1337 View Post
Sec putting my code up here which I actually used for my cod4 server tool.

Create a module(reading .ini)

Code:
Module Module1
    Public Function ReadIniValue(ByRef INIpath As String, ByRef KEY As String, ByRef Variable As String) As String
        Dim NF As Short
        Dim Temp As String
        Dim LcaseTemp As String
        Dim ReadyToRead As Boolean

AssignVariables:
        NF = FreeFile
        ReadIniValue = ""
        KEY = "[" & LCase(KEY) & "]"
        Variable = LCase(Variable)

EnsureFileExists:
        FileOpen(NF, INIpath, OpenMode.Binary)
        FileClose(NF)
        SetAttr(INIpath, FileAttribute.Archive)

LoadFile:
        FileOpen(NF, INIpath, OpenMode.Input)
        While Not EOF(NF)
            Temp = LineInput(NF)
            LcaseTemp = LCase(Temp)
            If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
            If LcaseTemp = KEY Then ReadyToRead = True
            If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
                If InStr(LcaseTemp, Variable & "=") = 1 Then
                    ReadIniValue = Mid(Temp, 1 + Len(Variable & "="))
                    FileClose(NF) : Exit Function
                End If
            End If
        End While
        FileClose(NF)
    End Function

End Module
Declare a .ini file:

Code:
Dim objIniFile As New IniFile(Application.StartupPath & "\data.ini")
Writing into the .ini file:

Code:
objIniFile.WriteString("Settings", "Whatever", Textbox1.Text)
        objIniFile.WriteString("Settings", "I need to pee", Textbox2.Text)
Reading from the .ini file:

Code:
Textbox1.Text.Text = ReadIniValue(Application.StartupPath & "\data.ini", "Settings", "Whatever")
        Textbox2.Text = ReadIniValue(Application.StartupPath & "\data.ini", "Settings", "I need to pee")
Dim objIniFile As New IniFile(Application.StartupPath & "\data.ini")
Gives me an error saying: IniFile is not declared
No (not fully) but it's cool, thanks for sharing, as long as problem is resolved
Not mine, but the issue is there is no INI class.

what was left out.

Create A class -->Doesn't matter what you call it, add the following code, and now you will get no error.

[php]
Public Class inifile
' API functions
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 System.Text.StringBuilder, _
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
Private 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
Private 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
Dim 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
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then GetString = _
Left(objResult.ToString, intCharCount)
End Function

Public Function GetInteger(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Integer) As Integer
' Returns an integer from your INI file
Return GetPrivateProfileInt(Section, Key, _
[Default], strFilename)
End Function

Public Function GetBoolean(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Boolean) As Boolean
' Returns a boolean from your INI file
Return (GetPrivateProfileInt(Section, Key, _
CInt([Default]), strFilename) = 1)
End Function

Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
' Writes a string to your INI file
WritePrivateProfileString(Section, Key, Value, strFilename)
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))
Flush()
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)))
Flush()
End Sub

Private Sub Flush()
' Stores all the cached changes to your INI file
FlushPrivateProfileString(0, 0, 0, strFilename)
End Sub

End Class
[/php]

Credits to Unknown, I think it is a conversion from vb5,vb6 CINI class, but I am not sure,
My class:

Code:
Public Class IniFile
    ' API functions
    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 System.Text.StringBuilder, _
      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
    Private 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
    Private 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
    Dim strFilename As String

    ' Constructor, accepting a filename
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    Public Sub WriteString(ByVal Section As String, _
      ByVal Key As String, ByVal Value As String)
        ' Writes a string to your INI file
        WritePrivateProfileString(Section, Key, Value, strFilename)
        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))
        Flush()
    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)))
        Flush()
    End Sub

    Private Sub Flush()
        ' Stores all the cached changes to your INI file
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub

End Class
Same as the one nextgen posted, sorry forgot =/
That should resolve all issues, and it is easy to forget , I hate modules and classes.... They're just so.............handy.
Posts 1–15 of 17 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us