vb.net - Most efficent way to save custom settings -
my vs2013 vb app saves settings registry , has grown point bothering me. want save settings (strings) files. bonus can on network , used multiple users. there may hundreds, thousands of lines. preferred format use? old ini, csv, xml? settings files don't appear option in case far can see.
i recommend creating object of class , use serialization xml .net applications. easy implement, supported , can import , export settings 1 computer another. can set application ready settings network. in addition, make future programming / upgrades easier.
i listing here working example ( tested ). code in use since 2012 , working enterprise level application.
note: included 1 " setting item " demo add own.
first : settings or options class :
imports system.xml.serialization imports system.io <serializable()> _ public class options public keys_use_lists boolean public sub new() keys_use_lists = true end sub #region "methods" public sub save(byval path string) try dim _xmlserilizer new xmlserializer(gettype(options)) dim _streamwriter new streamwriter(path) _xmlserilizer.serialize(_streamwriter, me) _streamwriter.close() catch ex exception msgbox("error saving database setting." & vbcrlf & "internal error: " & ex.message) end try end sub public function doload(byval path string) options dim _opt new options try if io.file.exists(path) dim _xmlserilizer new xmlserializer(gettype(options)) dim _filestream new filestream(path, filemode.open) _opt = directcast(_xmlserilizer.deserialize(_filestream), options) _filestream.close() end if catch ex exception msgbox("error loading database setting." & vbcrlf & "internal error: " & ex.message) end try return _opt end function #end region end class
second : use through interface ( winforms example )
public class frmoptions public _options new options private sub frmoptions_load(sender object, e eventargs) handles mybase.load if io.file.exists(application.startuppath & "/_options.opt") _options = _options.doload(application.startuppath & "/_options.opt") end if chkuselist.checked = _options.keys_use_lists end sub private sub btnok_click(sender object, e eventargs) handles btnok.click if io.file.exists(application.startuppath & "/_options.opt") _options = _options.doload(application.startuppath & "/_options.opt") end if _options.keys_use_lists = chkuselist.checked _options.save(application.startuppath & "/_options.opt") me.dialogresult = windows.forms.dialogresult.ok me.close() end sub end class
wiki
Comments
Post a Comment