Using My.settings in VB2005

G

Guest

Hello,

I have a few questions about the My.Settings user configuration.

Is it posible to change the filepath of the my.settings userconfig file ?

Is it posible to have different files so i can group my setting by type ?

In my Application i need to store a lot of different parameters
and i want a different file for each group.

Thanks in advanced

Hans
 
K

Ken Tucker [MVP]

Hi,

You can store the information in an xml file. You will not have use
of the my namespace to load the values.

Ken
 
G

Guest

Hello Ken,

Thanks for your answer,

What is the best way to read and write to the XML file, must i use a
XMLDocument or a xmldatadocument ?
 
J

Josef Brunner

Hi Hans,

for writing XML files I use

Private Function CreateXMLFile(ByVal FileName As String) As String

Try

' Create the name for the new XML file depending on the given name

Dim NewObjectFile As String = _NodesDir + "\" + FileName + XMLPostfix

Dim myFileInfo As FileInfo = New FileInfo(NewObjectFile)

' Check if a file with this name already exists

If myFileInfo.Exists = True Then

Return "error"

End If

' We passed all the syntax checks -- write the config to the new XML file

_XML = New XmlTextWriter(NewObjectFile, System.Text.Encoding.Default)

With _XML

' Activate Indented

..Formatting = Formatting.Indented

' Write start declarations

..WriteStartDocument()

' Start with root element

..WriteStartElement("config")

' Write the current date as comment

..WriteComment(Date.Now.ToString)

End With

Catch ex As Exception

Return ex.ToString

End Try

' Success

Return "0"

End Function






Private Function FinishXMLFile() As String

Try

With _XML

..WriteEndElement() ' Ends "config" node

..Flush()

..Close()

End With

Catch ex As Exception

End Try

' Success

Return "0"

End Function



Hope this helps,

Josef
 
G

Guest

Hi Josef,

Thanks,

Can you give me an example of how to read and write to an xml file >

Hans
 
J

Josef Brunner

Hi Hans,

Hans said:
Can you give me an example of how to read and write to an xml file >

use the functions provided in my last posting to create the object and start
writing an xml file and the other one to close everything. Then, for
writing, you can use something like this

With _XML

' Write data

..WriteStartElement("PortRange")

..WriteAttributeString("Name", RangeName)

..WriteAttributeString("Protocol", Protocol)

..WriteAttributeString("RangeStart", RangeStart.ToString)

..WriteAttributeString("RangeStop", RangeStop.ToString)

..WriteEndElement()

End With



and for reading:





If myFile.Exists = True Then

Dim xr As New XmlTextReader(myFile.FullName)

Do While xr.Read()

If xr.NodeType = XmlNodeType.Element Then

If String.Equals(xr.Name, "PortRange") Then

' This XML file holds a single generic port config

myPort.Type = "PortRange"

myPort.Name = xr.GetAttribute("Name")

myPort.Protocol = xr.GetAttribute("Protocol")

myPort.RangeStart = System.Convert.ToInt32(xr.GetAttribute("RangeStart"))

myPort.RangeStop = System.Convert.ToInt32(xr.GetAttribute("RangeStop"))

' Finish reading this file

Exit Do

End If

End If

Loop ' Loop through the XML file

xr.Close() ' Close the file we just read!

End If



Hope it helps

Josef
 
G

Guest

Hi Josef,

How do i make a xsd file and how do i validate the content of the xml file

Hans
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top