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 ?
Asked
Active
Viewed 383 times
3 Answers
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
-
As mentioned i have added my setting in Settings.setting file under properties.My question is where should the code Settings1.Default.UserName = textBox1.Text; Settings1.Default.Save(); be written ? – user2054702 Aug 21 '13 at 15:39
-
after successfull login you can save the username so user doesn't enter it for the next time and just enter the password and login – mahdi gh Aug 21 '13 at 15:42
-
do you have a login form or not? – mahdi gh Aug 21 '13 at 15:55
-
No.I am not using forms. – user2054702 Aug 21 '13 at 15:57
-
so you have to explain more.then how do u authenticate user? – mahdi gh Aug 21 '13 at 16:01
-
I am suing the properties namespace, but i am getting an error saying that it could not be found. – user2054702 Aug 21 '13 at 16:27
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