|
-
Jan 8th, 2004, 01:14 AM
#1
Thread Starter
Lively Member
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!
-
Jan 8th, 2004, 02:58 AM
#2
Addicted Member
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)
-
Jan 8th, 2004, 03:18 AM
#3
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]
-
Jan 8th, 2004, 09:17 AM
#4
Member
-
Jan 8th, 2004, 12:35 PM
#5
Thread Starter
Lively Member
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!
-
Jan 8th, 2004, 12:40 PM
#6
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>
-
Jan 8th, 2004, 01:13 PM
#7
Thread Starter
Lively Member
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.
-
Jan 8th, 2004, 02:05 PM
#8
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
Last edited by Edneeis; Jan 8th, 2004 at 02:10 PM.
-
Jan 8th, 2004, 02:22 PM
#9
Thread Starter
Lively Member
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
-
Jan 8th, 2004, 02:27 PM
#10
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.
-
Jan 8th, 2004, 02:47 PM
#11
Thread Starter
Lively Member
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!
-
Jan 8th, 2004, 02:53 PM
#12
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!
-
Jan 8th, 2004, 03:04 PM
#13
Thread Starter
Lively Member
Ok, so now I'm working on the user interface of a form. What happened to the "line control" that we had in VB6?
-
Jan 8th, 2004, 03:55 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|