Results 1 to 11 of 11

Thread: [ALMOST SOLVED] Text-Based Settings

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    23

    [ALMOST SOLVED] Text-Based Settings

    I want to make a program that uses text files (or whatever I choose the extension to be) for settings. I have an example from someone's SVN updater for a game. The following is in customaddons.ini. I want to be able to do something similar.

    Code:
    LuaMenu
    http://luastoned.googlecode.com/svn/trunk/luamenu/
    LuaMenu
    All pointers/help appreciated .
    Last edited by Fleamonji; Aug 2nd, 2010 at 09:02 AM.
    Self-taught 14 year old VB.NET, C#, HTML, CSS, and PHP programmer.

  2. #2
    Lively Member
    Join Date
    Jul 2010
    Posts
    89

    Re: Text-Based Settings

    so you would like to read a file from the net and get contents into a string/textbox/listview or what?

    whats in the text files? settings for what? oyu'll have to be a bit more clear

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    23

    Re: Text-Based Settings

    Quote Originally Posted by vistar86 View Post
    so you would like to read a file from the net and get contents into a string/textbox/listview or what?

    whats in the text files? settings for what? oyu'll have to be a bit more clear
    I want to store settings like "Previous Version", "Last Update Date", "Steam folder path", "Steam Username". I need to be able to read them from within the program, and display them.

    EDIT: And I don't want to use My.Settings
    Last edited by Fleamonji; Aug 1st, 2010 at 07:32 PM.
    Self-taught 14 year old VB.NET, C#, HTML, CSS, and PHP programmer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text-Based Settings

    We can't tell you how to do "whatever you choose". You have to choose something first and then we can tell you how to do it. First up, why don't you want to use My.Settings? There may well be a legitimate reason but many have misguidedly said the same before. If you explain FULLY your intent then we know we won;t be wasting our time and yours reinventing the wheel. Assuming that My.Settings is not the way to go, what EXACTLY do you want to do instead? Do you want to use an INI file? An XML file? A text file of your own format? Something else?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Text-Based Settings

    You don't have to use My.Settings but you can use the same format xml file and put it anywhere you want.

  6. #6
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Text-Based Settings

    I've always used *.ini files since VB3. They are easy to use and easy to read (both humans and computers). XML is good too, but it isn't easy to read and edit for non-programmers using our code. Does anyone else have any more modern approaches?

    The draw backs to using *.ini files is microsoft doesn't want us to use them anymore, so sooner or later they may not work anymore with out writing our own access methods.

    API declares
    Code:
        Private Declare Function GetPrivateProfileString _
           Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
           ByVal lpSectionName As String, _
           ByVal lpKeyName As String, _
           ByVal lpDefault As String, _
           ByVal lpbuffurnedString As String, _
           ByVal nBuffSize As Integer, _
           ByVal lpFileName As String) As Integer
    
        Private Declare Function WritePrivateProfileString Lib "kernel32" _
            Alias "WritePrivateProfileStringA" ( _
            ByVal lpApplicationName As String, _
            ByVal lpKeyName As String, _
            ByVal lpString As String, _
            ByVal lpFileName As String) As Integer
    Code exampes:
    Writing
    Code:
                    WritePrivateProfileString(strSection, strKey, strValue, strIniFilePath)
    For these values:strSection = "Setting", strKey = "MainTopLeft", strValue = 120, strIniFilePath = "C:\Program Files\TestApp.ini" will produce:

    [Settings]
    MainTopLeft=120


    Reading (have this wrapped in a nice function
    Code:
        Private Function GetValueForKey(ByVal strSection As String, ByVal strKey As String, Optional ByRef strDefault As String = vbNullString) As String
            '*****************************************************************************************
            '* PURPOSE:    Returns a value for a specific key from an ini file.                      *
            '*****************************************************************************************
            '* PARAMETERS: strSection - The section name to get a key from.                          *
            '*             strKey     - The key to get the value for.                                *
            '*             strDefault - The default to use if the value or key is not found.         *
            '*****************************************************************************************
            '* RETURNS:    A value associated with a specific key.                                   *
            '*****************************************************************************************
            Dim rc As Integer
            Dim strBuffer As String
    
    
            strBuffer = Space(mconBufferSize)'mconBufferSize is the maximum size of the data you expect to receive + 1
    
            rc = GetPrivateProfileString(strSection, strKey, strDefault, _
                strBuffer, Len(strBuffer), mstrIniFilePath)
            If rc Then
                GetValueForKey = Microsoft.VisualBasic.Left(strBuffer, rc)
            Else
                GetValueForKey = vbNullString
            End If
    
        End Function
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    23

    Re: Text-Based Settings

    Thanks, that's exactly what I needed <3.

    EDIT: I've never been able to get XML working, so I'll start with this. And to answer whoever above, I am going to be adding a feature to the program that will allow users to add an IP:Port and Name to a list, and get the server info/connect to it too. My.Settings wouldn't be useful for that.

    Code:
    strBuffer = Space(mconBufferSize) 'mconBufferSize is the maximum size of the data you expect to receive + 1
    
            rc = GetPrivateProfileString(strSection, strKey, strDefault, _
                strBuffer, Len(strBuffer), mstrIniFilePath)
    It's complaining about mconBufferSize and mstrIniFilePath not being declared or being inaccessible.
    Last edited by Fleamonji; Aug 2nd, 2010 at 11:05 AM.
    Self-taught 14 year old VB.NET, C#, HTML, CSS, and PHP programmer.

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Text-Based Settings

    Quote Originally Posted by Fleamonji View Post
    My.Settings wouldn't be useful for that.
    My.Settings is just a simple overlay to allow you to quickly read/write simple datatypes. You can very simply code a configurationmanager to an XML file to give you far more capability then an ini file ever could.

  9. #9
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: [ALMOST SOLVED] Text-Based Settings

    mconBufferSize is the maximum string length.
    mstrIniFilePath is the file path.

    You will have to supply your own values there.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  10. #10
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [ALMOST SOLVED] Text-Based Settings

    Just for fun I made a little xml reader to show what you can do with xml

    Code:
    Private Class clsServerInfo
    		Public ServerName As String
    		Public ServerIP As String
    		Public Setting1 As Integer
    
    		Public Sub ShowDetails()
    			MsgBox("We have " & ServerName & " on " & ServerIP & " with setttings " & Setting1.ToString)
    		End Sub
    	End Class
    
    	Private strFileName As String = "c:\myfile.xml"
    
    	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    		ListServers()
    
    		Dim strEntry As String = InputBox("Enter the name of a server", , "test")
    		WriteServerDetails(New clsServerInfo() With {.ServerName = strEntry, .ServerIP = "56.12.12.1", .Setting1 = 45})
    		ReadServerDetails(strEntry).ShowDetails()
    		WriteServerDetails(New clsServerInfo() With {.ServerName = strEntry, .ServerIP = "0.0.12.1", .Setting1 = 55})
    		ReadServerDetails(strEntry).ShowDetails()
    
    	End Sub
    
    	Private Sub ListServers()
    
    		Dim objXMLFile As New Xml.XmlDocument
    
    		If IO.File.Exists(strFileName) Then
    			objXMLFile.Load(strFileName)
    		End If
    
    		Dim objSettings As Xml.XmlNode = objXMLFile.SelectSingleNode("Settings")
    		If objSettings Is Nothing Then
    			objSettings = objXMLFile.AppendChild(objXMLFile.CreateElement("Settings"))
    		End If
    
    		Dim strItems As String = ""
    		For Each objEntry As Xml.XmlNode In objSettings.ChildNodes
    			strItems &= objEntry.Name & vbNewLine
    		Next
    		If strItems = String.Empty Then
    			MsgBox("No servers found")
    		Else
    			MsgBox("Found: " & vbNewLine & strItems)
    		End If
    
    
    	End Sub
    
    	Private Sub WriteServerDetails(ByVal ServerDetails As clsServerInfo)
    
    		Dim objXMLFile As New Xml.XmlDocument
    
    		If IO.File.Exists(strFileName) Then
    			objXMLFile.Load(strFileName)
    		End If
    
    		Dim objSettings As Xml.XmlNode = objXMLFile.SelectSingleNode("Settings")
    		If objSettings Is Nothing Then
    			objSettings = objXMLFile.AppendChild(objXMLFile.CreateElement("Settings"))
    		End If
    
    		'Check if the xmlfile already has this entry
    		Dim objXMLNode As Xml.XmlNode = objSettings.SelectSingleNode(ServerDetails.ServerName)
    		If objXMLNode Is Nothing Then
    			objXMLNode = objXMLFile.CreateElement(ServerDetails.ServerName)
    			objXMLNode.AppendChild(objXMLFile.CreateElement("ServerIP")).InnerText = ServerDetails.ServerIP
    			objXMLNode.AppendChild(objXMLFile.CreateElement("Setting1")).InnerText = ServerDetails.Setting1.ToString
    			objSettings.AppendChild(objXMLNode)
    		Else
    			objXMLNode.SelectSingleNode("ServerIP").InnerText = ServerDetails.ServerIP
    			objXMLNode.SelectSingleNode("Setting1").InnerText = ServerDetails.Setting1.ToString
    		End If
    
    		objXMLFile.Save(strFileName)
    
    	End Sub
    
    	Private Function ReadServerDetails(ByVal ServerName As String) As clsServerInfo
    
    		Dim objXMLFile As New Xml.XmlDocument
    
    		If IO.File.Exists(strFileName) Then
    			objXMLFile.Load(strFileName)
    		End If
    
    		Dim objSettings As Xml.XmlNode = objXMLFile.SelectSingleNode("Settings")
    		If objSettings Is Nothing Then
    			objSettings = objXMLFile.AppendChild(objXMLFile.CreateElement("Settings"))
    		End If
    
    		Dim objXMLNode As Xml.XmlNode = objSettings.SelectSingleNode(ServerName)
    		If Not objXMLNode Is Nothing Then
    			Dim objResult As New clsServerInfo
    			objResult.ServerName = ServerName
    			objResult.ServerIP = objXMLNode.SelectSingleNode("ServerIP").InnerText
    			objResult.Setting1 = CInt(objXMLNode.SelectSingleNode("Setting1").InnerText)
    			Return objResult
    		End If
    
    	End Function
    Last edited by Grimfort; Aug 2nd, 2010 at 11:42 AM. Reason: better version!

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Text-Based Settings

    Quote Originally Posted by Fleamonji View Post
    Thanks, that's exactly what I needed <3.

    EDIT: I've never been able to get XML working, so I'll start with this. And to answer whoever above, I am going to be adding a feature to the program that will allow users to add an IP:Port and Name to a list, and get the server info/connect to it too. My.Settings wouldn't be useful for that.

    Code:
    strBuffer = Space(mconBufferSize) 'mconBufferSize is the maximum size of the data you expect to receive + 1
    
            rc = GetPrivateProfileString(strSection, strKey, strDefault, _
                strBuffer, Len(strBuffer), mstrIniFilePath)
    It's complaining about mconBufferSize and mstrIniFilePath not being declared or being inaccessible.
    Why not?


    Also... you could just serialize to/from xml ... might be a bit saner and easier that dealing with the XML directly.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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