0

Is it possible to store simple data in a windows form? Similar to a cookie to store a value to tell what the last scraped ID was. Will the data remain when the app is reloaded?

I already have a database and can use that if needed but was looking for a simpler storage solution.

Thanks

Sian Jakey Ellis
  • 435
  • 1
  • 4
  • 13

3 Answers3

2

what I found was the easiest to store and use settings like that, was to create a serializable settings class; for example:

[Serializable()]
public class Settings()
{
    public object Something { get; set; }
}

and then, whenever you close your application, you can Serializable that class (meaning, save it as data somewhere)

And then on your application load event: deserializable the saved data, and you'll have everything back the way is was, as the same kinda objects before you closed your application.

Ron Sijm
  • 8,490
  • 2
  • 31
  • 48
  • Where would the best place to store it be? The easiest to access and update at runtime. – Sian Jakey Ellis Nov 04 '11 at 23:03
  • I suppose the best place to store it would be in the user isolated storage you pretty much always have read/write permissions to there, and you can have different settings per windows user with hardly any effort. – Ron Sijm Nov 04 '11 at 23:10
0

You have multiple option here.

Firstly you could write the value to a text file yourself using the StreamWriter class.

Another, you could looking into storing the value in an AppConfig file.

Id say the AppConfig route is probably the easiest. All you would do is create the file (see the link for doing that) then add this to the file.

<appSettings>
    <add key="SomeValue" value="SomeString" />
<appSettings>

Then use this to access the value.

ConfigurationSettings.AppSettings["SomeValue"]

And this to set it

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("SomeValue", "SomeString");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

Hope this points you in the right direction.

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
  • For reading and writing in file is better offer StreamReader and StreamWriter, but how you want keep dynamic data in app.config? – Saeed Amiri Nov 04 '11 at 22:47
  • @SaeedAmiri - I've change my answer to recommend a StreamWriter and explain how they could use the appconfig file – Ash Burlaczenko Nov 04 '11 at 22:56
  • You can read from app.config during execution but you can't write on it during execution and use it, as I understand OP want's save some data (for example password) which is dynamic and changes by different executions. – Saeed Amiri Nov 04 '11 at 22:59
  • @SaeedAmiri - Thats not correct, AppConfigs can be written to during runtime. – Ash Burlaczenko Nov 04 '11 at 23:07
  • No how do you want write on them? like files? so why use it, appconfig is good for static data during one runtime. (I mean by adding dynamic number of keys). – Saeed Amiri Nov 04 '11 at 23:11
  • app.config is not really for storing user specific settings like that tho, more for app behavior settings. Which is not really what he's asking for. – Ron Sijm Nov 04 '11 at 23:23
0

Consider using IsolatedStorage (see discussion When should I opt for IsolatedStorage versus AppData file storage? ) - http://msdn.microsoft.com/en-us/library/3ak841sy(v=VS.100).aspx.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179