Results 1 to 11 of 11

Thread: config file help

  1. #1
    Member
    Join Date
    Dec 11
    Posts
    34

    config file help

    wat is the best way to load and edit a config.ini file then save it?

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    Re: config file help

    If you want to load the whole file into your program and edit it as a text file then I would just use a TextBox with Multiline set to true, read the whole file and place it in the textbox, let the user make changes then save to file.

    For inputing the data you could use Line Input# in a loop and append each line to the textbox followed by a vbCRLF
    Or you could use the Input#() method and specify the size of the input by using the LOF(#FileNumber) as a parameter

    For writing you could just use Print# add a ; to the end of the line if you do not want an extra CRLF added to the end.

    This would be basically the same as loading and editiing in notepad so you may be better off just to use notepad.

    Now if you want something more specific than just a notepad view you may want to use the API to read and write values from.to the ini files.

    So in other words the best way depends on what you want to do and what you need it to look like.

  3. #3
    Member
    Join Date
    Dec 11
    Posts
    34

    Re: config file help

    na i want to just change like

    i want to make it so u have a settings page were u set them via check boxes and stuff but i want it to out put the setting set into the config.ini
    but on top of that theres gonna be 2 set of settings like i need to load the settings of bleh to the file then edit then save but i wanna be able to choose bleh2 and edit it as well the save it hopefully that makes sense

    [Bleh]
    Profile=BLEH
    Gamer=TIME

    [Bleh1]
    Profile=bleh
    Gamer=Time

  4. #4
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,152

    Re: config file help

    The usual method would be to use the GetPrivateProfileString and WritePrivateProfileString APIs. However you should note that idea of .ini files to hold configurations was superceded a while ago. The 'accepted' method is to use the inbuilt GetSetting Function and the SaveSetting Statement which read and write values to the Registry.

  5. #5
    Member
    Join Date
    Dec 11
    Posts
    34

    Re: config file help

    well i dont have the choice for the second option as its a program made to modify a config ini

    is there a way you can give me a indeth code for get pirvate string so i can understand wat i need to do and wat not im not that great with new stuff

  6. #6
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,152

    Re: config file help

    Here's a very simple example
    Code:
    Option Explicit
    
    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
    Private strIniFile As String
                        
    Private Sub cmdGetValues_Click()
    Dim lngReturn As Long
    Dim strSection As String
    Dim strKey As String
    Dim strDefault As String
    Dim strData As String
    Dim lngLen As Long
    strDefault = "NO VALUE"
    strSection = "Bleh"
    strKey = "Profile"
    strData = Space(255)
    lngLen = Len(strData)
    lngReturn = GetPrivateProfileString(strSection, strKey, strDefault, strData, lngLen, strIniFile)
    If lngReturn <> 0 Then
        strData = Mid$(strData, 1, lngReturn)
    Else
        strData = strDefault
    End If
    Debug.Print strData
    strSection = "Bleh1"
    strKey = "Gamer"
    strData = Space(255)
    lngReturn = GetPrivateProfileString(strSection, strKey, strDefault, strData, lngLen, strIniFile)
    If lngReturn <> 0 Then
        strData = Mid$(strData, 1, lngReturn)
    Else
        strData = strDefault
    End If
    Debug.Print strData
    End Sub
    
    Private Sub cmdSave_Click()
    Dim lngReturn As Long
    Dim strSection As String
    Dim strKey As String
    Dim strData As String
    strSection = "Bleh"
    strKey = "Profile"
    strData = "Doogle2"
    lngReturn = WritePrivateProfileString(strSection, strKey, strData, strIniFile)
    If lngReturn <> 0 Then
        MsgBox "Changes Saved"
    Else
        MsgBox "Error - " & Err.LastDllError & " When Saving Changes"
    End If
    End Sub
    
    Private Sub Form_Load()
    strIniFile = "C:\MyDir\Myini.ini"
    End Sub
    The .ini File looks like this
    Code:
    [Bleh]
    Profile = Doogle
    [Bleh1]
    Gamer = Doogle1
    Clicking on cmdGetValues will display 'Doogle' and 'Doogle1' to the Immediate Window. Clicking on cmdSave will change the value of the 'Profile' key in section 'Bleh' to 'Doogle2'

    EDIT: BTW strData must be large enough to hold the maximum length of the data to read and lngLen must be set to that length. In the example above I've used a maximum of 255 characters
    Last edited by Doogle; Aug 10th, 2012 at 12:26 AM. Reason: Assigned strIniFile in Form_Load event

  7. #7
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 04
    Location
    Staffordshire, England
    Posts
    2,091

    Re: config file help

    Quote Originally Posted by Doogle View Post
    The usual method would be to use the GetPrivateProfileString and WritePrivateProfileString APIs. However you should note that idea of .ini files to hold configurations was superceded a while ago. The 'accepted' method is to use the inbuilt GetSetting Function and the SaveSetting Statement which read and write values to the Registry.
    True but all that does is put unnecessary rubbish in the registry which can still be edited but users.

    I know ini files can easily be edited by anyone but they don't have to be config.ini it can be config.dll providing it has a filename it doesn't matter. You could also change its time and date when its used so users don't see its been written too.
    Keith

    I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.


  8. #8
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    Re: config file help

    I also do not like using the registery, IMO this was a horrible idea on the part of MS. The registery gets even more bloated over time and hurts the performance of the PC sometimes drastically plus the built in VB functions for registery access write to the current user section and can be an issue if someone else logs into the system. It has its uses but is overused and causes more problems than it solves.

    INI Files work well and do not adversly effect other programs running on the PC so IMO they are a better choice in most cases.

  9. #9
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 04
    Location
    Staffordshire, England
    Posts
    2,091

    Re: config file help

    I couldn't agree more DataMiser.
    Keith

    I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.


  10. #10
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,152

    Re: config file help

    Don't think i've ever used .ini files 'in anger', always used the registry.

    I have used the APIs to implement pseudo Random Access by key rather than RBA where a Database may not be appropriate. (just for fun)

  11. #11
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,957

    Re: config file help

    I've had systems slow to a crawl due to a bloated and fragmented registery. Seems kinda strange that MS would recommend that you store your 10-100 bytes of config data in a file that contains 10-50 megs of other stuff. No way that could ever be efficent. It just keeps growing and growing until you have a problem.

    Now it seems they want you to store your settings in an XML file. Maybe someday they will make up thier mind.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •