Store settings

Store settings

Ralf_Krieg
Advisor Advisor
694 Views
3 Replies
Message 1 of 4

Store settings

Ralf_Krieg
Advisor
Advisor

Hello

 

A customer would like to use a windows form in a rule. In addition to this, he wants the form NOT as compiled dll file. So I made two vb.net class files. One for the code and one to build the form gui. A rule creates a new instance of this form and shows it. All works fine. In the form is a listbox. The entries of the listbox should load on form initialize, should be editable on runtime and stored on closing form. And that's my problem. If I compile the form as a dll and load it, I can access My.Settings which read and store the listbox entries in the user.config file in users local app data folder. A straight class file can not use this way.

Any ideas for an alternative way?


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Accepted solutions (1)
695 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

When you need to use some settings stored in file, you need to implement your own "engine".

Usually I use XML or JSON file as storage, because there are many possibilities to store different settings with one engine in one file. But it requires too much coding for single use.

The easiest way for settings as array of strings in defined order you can use this simple code

 

 

Sub Main
	'Create instance of SimpleSettings class
	Dim mySimpleSettings = New SimpleSettings()

	'Load settings to new List
	Dim currentSettings As New List(Of String)(mySimpleSettings.ReadSettings())
	
	'Use (print) existing settings
	Logger.Debug("Settings loaded from " & mySimpleSettings.ConfigFile)
	Logger.Debug(String.Join(vbCrLf, currentSettings))

	'Create/Change/Delete settings
	currentSettings.Add("newSetting" & currentSettings.Count)

	'Save settings
	mySimpleSettings.SaveSettings(currentSettings.ToArray())
End Sub


Class SimpleSettings
	
	Public ReadOnly ConfigFile As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) & "\MySettings.cfg"

	Public Sub SaveSettings(ParamArray settings As String())
		System.IO.File.WriteAllLines(ConfigFile, settings)
	End Sub

	Public Function ReadSettings() As String()
		If System.IO.File.Exists(ConfigFile) Then
			Return System.IO.File.ReadAllLines(ConfigFile)
		Else
			Return New String() {}
		End If
	End Function
	
End Class

 

 

 

 

Message 3 of 4

Ralf_Krieg
Advisor
Advisor

Hello

 

Thanks, that push me in right direction. Created a simple serializable class and subs to read and write to/from XML. The path to the XML file is hard coded in the base rule.


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 4 of 4

Michael.Navara
Advisor
Advisor
0 Likes