-1

I am trying to persist the username for my PC client.I have an setting called Username, type string, scope user. Could someone please tell me where do i assign value to this setting in run time and how do i persist it ?

user2054702
  • 69
  • 1
  • 11

3 Answers3

1

Add a setting file to your project. then add a username property with the scope of user.

this way you can set the value and save it for the next time.

 Settings1.Default.UserName = textBox1.Text;
 Settings1.Default.Save();
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
mahdi gh
  • 438
  • 1
  • 7
  • 18
0

In Winforms, it is available under Settings in the same namespace as your Form.

Settings.Default.Username = "My Username";
Settings.Default.Save();
Khan
  • 17,904
  • 5
  • 47
  • 59
  • Where should the following lines of code be written ?Settings.Default.UserName = username; Settings.Default.Save(); – user2054702 Aug 21 '13 at 15:52
0

App Settings can be overwritten by application

You can do it like this:

string username = txtUser.Text;
Settings.Default.Username = username;
Settings.Default.Save();

Edit: If this is a login form, you can add this piece of code to the submit event method, probably linked to a button.

private void btnSubmit_Click(object sender, EventArgs e)
{
    string username = txtUser.Text;
    Settings.Default.Username = username;
    Settings.Default.Save();
}

If you want to bypass login on form loading (and if this is why you want to store username), you can check if the user is saved right at the form load event

private void frmMyForm_Load(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(Settings.Default.Username))
    {
        //start you application and bypass login
    }
    else
    {
        //show login form
    }
}
Gustavo Azevedo
  • 113
  • 1
  • 7
  • Where should the following lines of code be written ?Settings.Default.UserName = username; Settings.Default.Save(); – user2054702 Aug 21 '13 at 15:50