Click here to Skip to main content
14,239,281 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi folks
i have a c# application that requires the user tom type his name in when he first runs the program. We wish to save this username always until he decides to delete the program. I want to make it that it saves it because there is an option of sending the log file to the developers and we need to know who it came from.I am using winforms.

What I have tried:

i tried saving it in a file that reads the text and if it is different it saves it but it does not save it if the program gets closed by visual studio.
Updated 1-May-18 4:17am
F-ES Sitecore 1-May-18 8:46am
You'll need to save the value (in a file or the registry) when the textbox content changes so that it is captured right away.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Easiest way is when the user enters it, save it into the application settings:
1) In the solution pane, open the project branch.
2) Open the Properties twig, and double click "Settings.Settings"
3) In the page that is displayed:
3.1) Under "Name" enter "UserName"
3.2) Under "Type" select "String"
3.3) Under "Scope" select "User"
3.4) Under "Value" leave it blank.
4) Close the page, and save the changes.
5) Now in your code at startup do this:
string userName = Properties.Settings.Default.UserName;
if (string.IsNullOrWhiteSpace(userName))
    ... Insert your code to get the user name ...
    Properties.Settings.Default.UserName = userName;
JDMCODEBLOX 1-May-18 11:06am
this helped also i tried it both ways and after working my code it works wonderfully
Thanx A Million!!!
OriginalGriff 1-May-18 11:09am
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 2

As I read things the real issue is that you want to send a users name with an error log to the developers. The better approach is to capture their login name instead of having them type in some text and then saving it. you can get their login name from Environment.UserName.

A better solution is to use a product like log4net or nLog to capture information about the error. These products allow you to save the information to a file or a database or both. These are nugget packages that you can add to your solution.
JDMCODEBLOX 1-May-18 10:36am
thanks alot thats what i was looking for

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100