Results 1 to 14 of 14

Thread: editing an .ini file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2003
    Posts
    74

    editing an .ini file

    The old VB6 way, we could use API calls to edit .ini files. Is there anything new in VB.NET that will allow me to edit .ini files without using API?

    Any help is appreciated. Thank you!

  2. #2
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    In .Net it is prefered you use xml files to .ini files for your applications configuration settings.

    If you still want to edit an ini file you can use the classes in .Net that deal with file. (FileStream for example, www.msdn.microsoft.com for more information)

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you can still ( and sometimes it's the only way ) use api's in vb.net / C# you know
    check this thread ( it shows how i use GetPrivateProfileString in .net ) INI
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4
    Member
    Join Date
    Nov 2003
    Location
    Amsterdam, The Netherlands
    Posts
    53
    try this..

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2003
    Posts
    74
    Thanks to all who replied.

    Carnifex, why is xml preferred over and ini file? All I need is 3 entries. Just a straight

    [something]
    key1=value1
    key2=value2
    key3=value3

    What advantages would I have using an xml file?

    Thanks!

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    For one you would need to use APIs to work with XML. XML is self describing so by its very nature it is easy to work with and well suited for things that used to be handled in INIs. It is still just text and thus gains all the advantages that INIs do from that fact but its structure is standardized and it has far more tools to work with it.

    <something>
    <key name="1" value="1"/>
    <key name="2" value="2"/>
    <key name="3" value="3"/>
    </something>

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2003
    Posts
    74
    Can someone direct me to some code to edit xml files. I will need to search for a key (tag name) and edit its value.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you decide to go the xml route then there are lots of options. If you use a .NET app.config file then there is a specific layout it follows but the Framework also provides classes to work with it and make life easy. Unforunately it only provides ways to read the settings in an app.config file not write to it, but there are an assortment of classes other people have written to do that. Then there are also tools to just work with xml. I like the XMLDocument object although if you only plan on using xml as a holder of settings you could even encapsulate it into your own class. Another option would be to write an options class and just serialize/deserialize it to xml for storage. Anyway here is an example using the XMLDocument object and the xml outline previously shown.
    VB Code:
    1. Dim xml As String = "<something><key name=""1"" value=""1""/>" & _
    2.         "<key name=""2"" value=""2""/><key name=""3"" value=""3""/></something>"
    3.         Dim doc As New Xml.XmlDocument
    4.         doc.LoadXml(xml)
    5.         'to load an xml file instead use:
    6.         'doc.Load("filepath.xml")
    7.         'get the key node with the name 3
    8.         Dim node As Xml.XmlNode = doc.SelectSingleNode("//key[@name='3']")
    9.         If Not node Is Nothing Then
    10.             'get the current name, not required to set a value
    11.             MsgBox(node.Attributes("name").Value)
    12.             'set the value of the found node
    13.             node.Attributes("value").Value = 5
    14.         End If
    15.         'save changes to file
    16.         'doc.Save("filepath.xml")

    A good resource for learning about how to query xml is http://www.w3schools.com/xpath/xpath...p?output=print
    Last edited by Edneeis; Jan 8th, 2004 at 02:10 PM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2003
    Posts
    74
    Thanks!

    Ok, now suppose my xml has the following structure:

    <something>
    <key1>value1</key1>
    <key2>value2</key2>
    <key3>value3</key3>
    </something>

    Can I then use the InnerText property to modify the value of the tag or is there a better way?

    Dim doc As New Xml.XmlDocument
    dim oNode as XmlNode

    doc.LoadXml(xml)
    oNode = doc.SelectSingleNode("//version")
    oNode.InnerText = "4.0.0.1"
    doc.save(xml)

    That seemed to easy. Is there anything I need to check or whatever? To me it seems like there should be more code, but the above did it for me.... =P

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Nope thats it you were spot on with the InnerText. Value is for attributes. The difference between whether or not to use attributes or nodes themselves is really just preference no plus or minuses either way. I think you'll be glad you switched to xml in the future. At least for me I found it more and more useful once I learned how to use it.

    PS. You may want to test if oNode is nothing in case it wasn't found but its not required. Also one thing to remember about xml is that its case sensitive in its node/attribute names.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2003
    Posts
    74
    Awesome! Thank you very much. You've answered some questions that I forgot to post... what's the difference between attribute & node, and why can't I edit the "value" property??

    Thanks a bunch!

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by yatingg
    Awesome! Thank you very much. You've answered some questions that I forgot to post... what's the difference between attribute & node, and why can't I edit the "value" property??

    Thanks a bunch!
    Thats because I am reading your mind!

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2003
    Posts
    74
    Ok, so now I'm working on the user interface of a form. What happened to the "line control" that we had in VB6?

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Its 'no longer with us'. I belive Hellswraith has a replacement control on his website though.

    http://www.variantx.com/Main/DevTool...neControl.aspx

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