-4

I have a message prompting the user to upgrade some equipment connected to the computer. I want to let the user disable this message in the future, by checking a box before pressing cancel.

How can I store this user option, so that next time the program executes I can avoid showing this message based on the user choice made in the last session of the application?

chwi
  • 2,752
  • 2
  • 41
  • 66
  • Registry? User Settings? – D Stanley Nov 14 '13 at 13:51
  • Read the documentation about the isolate storage class (System.IO.IsolatedStorage). This could be useful for small amounts to store. – nabuchodonossor Nov 14 '13 at 13:52
  • Object Serialization? – Sudhakar Tillapudi Nov 14 '13 at 13:53
  • @DStanley Maybe its a user setting? I dont know, I have very little experience. – chwi Nov 14 '13 at 13:54
  • The simplest way (not necessarily the best) is probably just using an app.config: http://stackoverflow.com/questions/13043530/what-is-app-config-in-c-nethow-to-use-it. That question shows how to read from it, but you can also set keys in it. – valverij Nov 14 '13 at 13:54
  • @Sudhakar Maybe? Could you elaborate on this? – chwi Nov 14 '13 at 13:54
  • @downvoters; could you please give a comment on the downvote so that I may know how to improve my question? – chwi Nov 14 '13 at 13:56
  • @Wilhelmsen I didn't downvote but probably because it's primarily an opinion-based question and you haven't shown what you've tried so far. – D Stanley Nov 14 '13 at 14:07
  • @DStanley I haven't tried anything because I really dont know where to start. Maybe next time I should act a fool, trying to store it in a variable just to try something, even though I know its not working... – chwi Nov 14 '13 at 14:26

2 Answers2

2

The two cleanest ways are the Registry and User Settings.

I prefer User Settings because:

  1. they're in XML (no registry hacking required)
  2. a lot of the grunt work is done by the framework
  3. User settings persist across upgrades automatically with ClickOnce

All you need to do is go to the Setings tab in the project's properties, add a setting, and set it's type to User.
Then just save the settings after changing them:

Properties.Settings.Default.ShowDisconnectMessage = false;
Properties.Settings.Default.Save();

The registry works similarly but it requires a bit more code and is not strongly-typed:

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software",true);
Key = key.OpenSubKey("AppName", true);
key.SetValue("ShowDisconnectMessage ", "false");
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

You'll need to make the messages in your application use a custom form. That form will need to show the message as well as have the check box. Then you'll want to store that information somewhere. I'm going to say in the application configuration file.

So, to save the data in the configuration file, first build a few extension methods to make it easier:

public static class Extensions
{
    public static void SetValue(this KeyValueConfigurationCollection o,
        string key,
        string val)
    {
        if (!o.AllKeys.Contains(key)) { o.Add(key, val); }
        else { o[key].Value = val; }
    }

    public static string GetValue(this NameValueCollection o,
        string key,
        object defaultVal = null)
    {
        if (!o.AllKeys.Contains(key)) { return Convert.ToString(defaultVal); }
        return o[key];
    }
}

Then, when you want to read that value to determine if you ought to show the message, do this:

var val = (bool)ConfigurationManager.AppSettings.GetValue("ShowTheMessage");

and then when you want to save the value, do this:

var config = ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.None);
var app = config.AppSettings.Settings;

app.SetValue("ShowTheMessage", checkBox.Checked);

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232