- Right click on your project link, then click on "Properties"
- Navigate into "Settings" tab, then add a string parameter. i.e. "ProjectOptions"
- The idea is to store multiple Key/Value properties in a single variable using XML format.
- Use the function SetProjectOption to store options
Public Sub SetProjectOption(ByRef ProjectOptions As String, ByVal Key As String, ByVal Value As String) Dim Options As Dictionary(Of String, String) Options = Me.GetDictoinary(ProjectOptions) If Options.ContainsKey(Key.ToLower) Then Options(Key) = Value Else Options.Add(Key.ToLower, Value) End If Me.storeDictionaryInString(Options, ProjectOptions) End Sub
- Use the function GetProjectOption to get options values
Public Function GetProjectOption(ByRef ProjectOptions As String, ByVal Key As String) As String Try Dim Options As Dictionary(Of String, String) Options = Me.GetDictoinary(ProjectOptions) Return Options(Key.ToLower) Catch ex As Exception Return Nothing End Try End Function
- Helper Function:
Private Function storeDictionaryInString(ByRef dic As Dictionary(Of String, String), ByRef ProjectOption As String) As String Dim doc As New XmlDocument() doc.CreateXmlDeclaration("1.0", "utf-8", Nothing) doc.AppendChild(doc.CreateElement("AllSettings")) Dim root As XmlNode = doc.GetElementsByTagName("AllSettings")(0) For Each key As String In dic.Keys Dim e As XmlElement = doc.CreateElement(key) e.InnerText = dic(key) root.AppendChild(e) Next ProjectOption = doc.OuterXml Return doc.OuterXml End Function Private Function GetDictoinary(ByRef ProjectOption As String) As Dictionary(Of String, String) Try Dim doc As New XmlDocument() doc.LoadXml(ProjectOption) Dim dic As New Dictionary(Of String, String) Dim root As XmlNode = doc.GetElementsByTagName("AllSettings")(0) For Each n As XmlNode In root.ChildNodes dic.Add(n.Name.ToLower, n.InnerText) Next Return dic Catch ex As Exception Return New Dictionary(Of String, String) End Try End Function
1. Store Options
Me.SetProjectOption(My.Settings.ProjectOptions, "General.CompanyName", "ABC Company Inc") Me.SetProjectOption(My.Settings.ProjectOptions, "General.PurchasedDate", "27 Dec 2017") Me.SetProjectOption(My.Settings.ProjectOptions, "General.OFlAG", "True")
2. Read Options
3. How options stored in the variable:MsgBox(Me.GetProjectOption(My.Settings.ProjectOptions, "General.OFlAG"))
"1.0" encoding="UTF-8" xml version= <AllSettings> <general.companyname>ABC Company Inc</general.companyname> <general.purchaseddate>27 Dec 2017</general.purchaseddate> <general.oflag>True</general.oflag> </AllSettings>