Results 1 to 9 of 9

Thread: [2008]Read/Write INI

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Question [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008]Read/Write INI

    try this. use a filename for your own .ini file, or nothing for win.ini

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim iFile As New IniFile(Nothing) 'My.Application.Info.DirectoryPath & "\test.ini")
    3.  
    4.         ''string
    5.         'iFile.WriteString("Settings", "ClockTime", "12:59")
    6.         'MsgBox(iFile.GetString("Settings", "ClockTime", "(none)"))
    7.  
    8.         ''integer
    9.         'iFile.WriteInteger("Settings", "test", 100)
    10.         'MsgBox(iFile.GetInteger("Settings", "test", 0))
    11.  
    12.         ''boolean
    13.         'iFile.WriteBoolean("Settings", "test", True)
    14.         'MsgBox(iFile.GetBoolean("Settings", "test", False))
    15.     End Sub

    vb Code:
    1. Public Class IniFile
    2.  
    3.     Private strFilename As String
    4.  
    5.     ' Constructor, accepting a filename
    6.     Public Sub New(ByVal Filename As String)
    7.         strFilename = Filename
    8.     End Sub
    9.  
    10.     ' Read-only filename property
    11.     ReadOnly Property FileName() As String
    12.         Get
    13.             Return strFilename
    14.         End Get
    15.     End Property
    16.  
    17.     Public Function GetString(ByVal Section As String, _
    18.       ByVal Key As String, ByVal [Default] As String) As String
    19.         If FileName IsNot Nothing Then
    20.             ' Returns a string from your INI file
    21.             Dim intCharCount As Integer
    22.             Dim objResult As New System.Text.StringBuilder(256)
    23.             intCharCount = API.GetPrivateProfileString(Section, Key, _
    24.                [Default], objResult, objResult.Capacity, strFilename)
    25.             If intCharCount > 0 Then
    26.                 Return Left(objResult.ToString, intCharCount)
    27.             Else
    28.                 Return String.Empty
    29.             End If
    30.         Else
    31.             ' Returns a string from your win.INI file
    32.             Dim intCharCount As Integer
    33.             Dim objResult As New System.Text.StringBuilder(256)
    34.             intCharCount = API.GetProfileString(Section, Key, _
    35.                 [Default], objResult, objResult.Capacity)
    36.             If intCharCount > 0 Then
    37.                 Return Left(objResult.ToString, intCharCount)
    38.             Else
    39.                 Return String.Empty
    40.             End If
    41.         End If
    42.     End Function
    43.  
    44.     Public Function GetInteger(ByVal Section As String, _
    45.       ByVal Key As String, ByVal [Default] As Integer) As Integer
    46.         If FileName IsNot Nothing Then
    47.             ' Returns an integer from your INI file
    48.             Return API.GetPrivateProfileInt(Section, Key, _
    49.                [Default], strFilename)
    50.         Else
    51.             ' Returns an integer from your win.INI file
    52.             Return API.GetProfileInt(Section, Key, _
    53.                [Default])
    54.         End If
    55.     End Function
    56.  
    57.     Public Function GetBoolean(ByVal Section As String, _
    58.       ByVal Key As String, ByVal [Default] As Boolean) As Boolean
    59.         If FileName IsNot Nothing Then
    60.             ' Returns a boolean from your INI file
    61.             Return CBool(API.GetPrivateProfileInt(Section, Key, _
    62.                CBool([Default]), strFilename))
    63.         Else
    64.             ' Returns a boolean from your win.INI file
    65.             Return CBool(API.GetProfileInt(Section, Key, _
    66.                CBool([Default])))
    67.         End If
    68.     End Function
    69.  
    70.     Public Sub WriteString(ByVal Section As String, _
    71.       ByVal Key As String, ByVal Value As String)
    72.         If FileName IsNot Nothing Then
    73.             ' Writes a string to your INI file
    74.             API.WritePrivateProfileString(Section, Key, Value, strFilename)
    75.         Else
    76.             ' Writes a string to your win.INI file
    77.             API.WriteProfileString(Section, Key, Value)
    78.         End If
    79.         Flush()
    80.     End Sub
    81.  
    82.     Public Sub WriteInteger(ByVal Section As String, _
    83.       ByVal Key As String, ByVal Value As Integer)
    84.         ' Writes an integer to your INI file
    85.         WriteString(Section, Key, CStr(Value))
    86.     End Sub
    87.  
    88.     Public Sub WriteBoolean(ByVal Section As String, _
    89.       ByVal Key As String, ByVal Value As Boolean)
    90.         ' Writes a boolean to your INI file
    91.         WriteString(Section, Key, CStr(CInt(Value)))
    92.     End Sub
    93.  
    94.     Private Sub Flush()
    95.         ' Stores all the cached changes to your INI file
    96.         API.FlushPrivateProfileString(0, 0, 0, strFilename)
    97.     End Sub
    98.  
    99. End Class
    100.  
    101. Public Class API
    102.  
    103.     ' API functions
    104.     Public Declare Ansi Function GetPrivateProfileString _
    105.       Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
    106.       (ByVal lpApplicationName As String, _
    107.       ByVal lpKeyName As String, ByVal lpDefault As String, _
    108.       ByVal lpReturnedString As System.Text.StringBuilder, _
    109.       ByVal nSize As Integer, ByVal lpFileName As String) _
    110.       As Integer
    111.     Public Declare Ansi Function WritePrivateProfileString _
    112.       Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    113.       (ByVal lpApplicationName As String, _
    114.       ByVal lpKeyName As String, ByVal lpString As String, _
    115.       ByVal lpFileName As String) As Integer
    116.     Public Declare Ansi Function GetPrivateProfileInt _
    117.       Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
    118.       (ByVal lpApplicationName As String, _
    119.       ByVal lpKeyName As String, ByVal nDefault As Integer, _
    120.       ByVal lpFileName As String) As Integer
    121.     Public Declare Ansi Function FlushPrivateProfileString _
    122.       Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    123.       (ByVal lpApplicationName As Integer, _
    124.       ByVal lpKeyName As Integer, ByVal lpString As Integer, _
    125.       ByVal lpFileName As String) As Integer
    126.  
    127.     Public Declare Function WriteProfileString Lib "kernel32" _
    128.     Alias "WriteProfileStringA" (ByVal lpszSection As String, _
    129.     ByVal lpszKeyName As String, ByVal lpszString As String) _
    130.     As Integer
    131.     Public Declare Function GetProfileString Lib "kernel32" Alias _
    132.     "GetProfileStringA" (ByVal lpAppName As String, _
    133.     ByVal lpKeyName As String, ByVal lpDefault As String, _
    134.     ByVal lpReturnedString As System.Text.StringBuilder, _
    135.     ByVal nSize As Integer) As Integer
    136.     Public Declare Function GetProfileInt Lib "kernel32" _
    137.     Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal _
    138.     lpKeyName As String, ByVal nDefault As Integer) As Integer
    139.  
    140. End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008]Read/Write INI

    did that help?

    just paste the class into your project + use it as shown in 1st code section

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]Read/Write INI

    Quote 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

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]Read/Write INI

    Quote 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

  8. #8
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    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.

  9. #9
    Member
    Join Date
    May 2005
    Posts
    32

    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
  •  



Click Here to Expand Forum to Full Width