Home All Groups Group Topic Archive Search About

Using My.settings in VB2005

Author
24 Mar 2006 3:52 PM
Hans
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

Author
27 Mar 2006 3:39 AM
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
----------------
Show quoteHide quote
"Hans" <H***@discussions.microsoft.com> wrote in message
news:EB02903E-41E8-4B07-850F-03146C7F898E@microsoft.com...
> 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
Author
27 Mar 2006 6:49 AM
Hans
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 ?

Show quoteHide quote
"Ken Tucker [MVP]" wrote:

> Hi,
>
>         You can store the information in an xml file.  You will not have use
> of the my namespace to load the values.
>
> Ken
> ----------------
> "Hans" <H***@discussions.microsoft.com> wrote in message
> news:EB02903E-41E8-4B07-850F-03146C7F898E@microsoft.com...
> > 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
>
>
>
Author
27 Mar 2006 8:15 AM
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

Show quoteHide quote
"Hans" <H***@discussions.microsoft.com> schrieb im Newsbeitrag
news:2334B8D3-C8BE-4C76-B5B0-281738B1B68D@microsoft.com...
> 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 ?
>
> "Ken Tucker [MVP]" wrote:
>
>> Hi,
>>
>>         You can store the information in an xml file.  You will not have
>> use
>> of the my namespace to load the values.
>>
>> Ken
>> ----------------
>> "Hans" <H***@discussions.microsoft.com> wrote in message
>> news:EB02903E-41E8-4B07-850F-03146C7F898E@microsoft.com...
>> > 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
>>
>>
>>
Author
27 Mar 2006 10:01 AM
Hans
Hi Josef,

Thanks,

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

Hans


Show quoteHide quote
"Josef Brunner" wrote:

> 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
>
> "Hans" <H***@discussions.microsoft.com> schrieb im Newsbeitrag
> news:2334B8D3-C8BE-4C76-B5B0-281738B1B68D@microsoft.com...
> > 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 ?
> >
> > "Ken Tucker [MVP]" wrote:
> >
> >> Hi,
> >>
> >>         You can store the information in an xml file.  You will not have
> >> use
> >> of the my namespace to load the values.
> >>
> >> Ken
> >> ----------------
> >> "Hans" <H***@discussions.microsoft.com> wrote in message
> >> news:EB02903E-41E8-4B07-850F-03146C7F898E@microsoft.com...
> >> > 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
> >>
> >>
> >>
>
>
>
Author
27 Mar 2006 11:25 AM
Josef Brunner
Hi Hans,

"Hans" <H***@discussions.microsoft.com> schrieb im Newsbeitrag
news:37DEDD69-639F-4A66-A19F-9AC63CC5E292@microsoft.com...
> 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
Author
27 Mar 2006 12:57 PM
Hans
Hi Josef,

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

Hans

Show quoteHide quote
"Josef Brunner" wrote:

> Hi Hans,
>
> "Hans" <H***@discussions.microsoft.com> schrieb im Newsbeitrag
> news:37DEDD69-639F-4A66-A19F-9AC63CC5E292@microsoft.com...
> > 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
>
>
>
Author
27 Mar 2006 1:11 PM
Josef Brunner
Hans,

xml validation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconReadingXMLWithXmlReader.asp

(I've never used this class...)

I'm sorry, but I did not have the need to work with xsd files yet...
Bye,
Josef

Show quoteHide quote
"Hans" <H***@discussions.microsoft.com> schrieb im Newsbeitrag
news:19030D44-405E-47EA-8678-4B2E075992C3@microsoft.com...
> Hi Josef,
>
> How do i make a xsd file and how do i validate the content of the xml file
>
> Hans
>
> "Josef Brunner" wrote:
>
>> Hi Hans,
>>
>> "Hans" <H***@discussions.microsoft.com> schrieb im Newsbeitrag
>> news:37DEDD69-639F-4A66-A19F-9AC63CC5E292@microsoft.com...
>> > 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
>>
>>
>>