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!
Printable View
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!
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)
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
try this..
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!
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>
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.
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:
Dim xml As String = "<something><key name=""1"" value=""1""/>" & _ "<key name=""2"" value=""2""/><key name=""3"" value=""3""/></something>" Dim doc As New Xml.XmlDocument doc.LoadXml(xml) 'to load an xml file instead use: 'doc.Load("filepath.xml") 'get the key node with the name 3 Dim node As Xml.XmlNode = doc.SelectSingleNode("//key[@name='3']") If Not node Is Nothing Then 'get the current name, not required to set a value MsgBox(node.Attributes("name").Value) 'set the value of the found node node.Attributes("value").Value = 5 End If 'save changes to file 'doc.Save("filepath.xml")
A good resource for learning about how to query xml is http://www.w3schools.com/xpath/xpath...p?output=print
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
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.
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! ;)Quote:
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!
Ok, so now I'm working on the user interface of a form. What happened to the "line control" that we had in VB6?
Its 'no longer with us'. I belive Hellswraith has a replacement control on his website though.
http://www.variantx.com/Main/DevTool...neControl.aspx