简体   繁体   中英

A way to save a specific My.Settings Variable in VB.NET

in My.Settings i've alot of variables including a 36000 records Datatable and Couple Large Array Lists, now when i call My.Settings.Save() it saves all the variables which takes alot of time and it saves variables that haven't been changed, is there a way make it Save only a Specific Datatable or ArrayList?

My.Setting.Save() 
'but what i want for example is something like
My.Settings.SomeDatatable.Save()

These settings are stored in an XML file. Since XML files do not store data in a fixed-width record format, there is no way to update a single node in an XML file without rewriting the whole thing. If it is too slow, I would recommend either:

  • Store these settings in multiple XML files, so that you only have to save the applicable file, but not the rest
  • Store the settings in the database where each row can be saved individually without rewriting the whole file

Databases store their data in fixed-width "pages" so that only the modified pages need to be rewritten, rather than the whole file.

What about saving your dataTable not within your Settings . You could make use of DataTable.WriteXml() . this would keep your settings much smaller and you can access them separately!

' save table
DataTable.WriteXml("yourFile.xml")

' load table
Dim newTable As New DataTable
newTable.ReadXml(fileName)

But if there is no other possibility this solution from SLaks will work too

' save table
Dim writer As New StringWriter()
table.WriteXml(writer)
My.MySettings.Default.TableXml = writer.ToString()

' load table
Dim reader As New StringReader(My.MySettings.Default.TableXml)
table.ReadXml(reader)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM