Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Your application may need global settings. These are typically stored in the configuration file and loaded at startup time. These settings affect the entire application, regardless of who is using the application on the local computer. You may also want to store user-specific settings. Each user on the local computer would get their own copy of the configuration value. .NET has a configuration API. Visual Studio gives you an editing tool to make it simple to access this configuration API. I have opened a project called SettingsDemo.
It contains three projects. I'm going to start by looking in SimpleConsole. SimpleConsole has no settings at this moment. To add a setting to this application, I can double-click on Properties and then go to the Settings tab. Visual Studio is telling me that this project doesn't contain a default settings file. I'm going to click on this hyperlink to add one. I want you to see what happened over here. The Properties node now has a Settings.settings item added here. Underneath that is a Settings.Designer.cs.
Here, I can create brand-new settings. I'm going to type in a new setting value here. I'll call this one TemplateLocation. It's going to be string, and the scope for that is going to be an application-wide setting. Down here, I'm going to add a HelpWebsite. Then of course, I can add some default strings over here. So I could come over here and type in http:, like so.
Now I want you to see what has written for me. There is a new app.config over here that didn't exist before. This app.config will be compiled and saved with my exe. When my application starts up, it'll read the contents of this app.config. Let me show you what's inside the app.config. There is now a new applicationSettings in here. Then there is a TemplateLocation setting with no current value in it and a HelpWebsite setting with the String I added a few minutes ago as the value.
These are read once when the application starts up. If you make any changes, they won't be read by your application until you shut down the application and restart it. Also, notice that over here in the Properties window, under Settings > Designer, Visual Studio is automatically creating some code on my behalf. It created a property called TemplateLocation. It's a read-only property. It created a property called HelpWebsite.
Now if I come back to this, and I add a user setting-- let me go back and double- click on Settings.setting-- and I set it to User specific setting, there is a new section called userSettings, and has added this section down here. Now this can be a read and write setting. It's stored here. But when you save the file, it's going to create a copy for each user and store their user-specific settings in a user identified folder.
To show you that, I'm going to move to this other application, SettingsDemo. I'm going to choose Window > Close All Documents. Then I'm going to move into SettingsDemo. This is my startup project. Here I'm going to run some code to read these settings. The data is accessible. These classes were created automatically for me. I access that by using Properties.Settings. Then I use the Default keyword and then the name of these settings. Now you're probably asking yourself, where did the UserAge property come from? Well, it's over here in that Settings area.
Inside this project, there is a Username, which has a Scope User, and the UserAge, which is an integer datatype. Down here is a Max App File, which is a long datatype, and a color, and so on. I'm able to access those datas through this class. So I'm going to read the values down here and store them in the Console's ForegroundColor. Then I'm going to print out the UserAge and the maximum files.
Then I'm going to show you the location of where those files are stored. So I already have a breakpoint over here. I'll press F5 to run the application. It hits my breakpoint, and then press F11 to step through the code. Here, I'm reading these settings. The UserConsole setting is green. No, I don't want to continue to be notified of his automatic step over. Thank you very much. Then this shows the location.
So my settings are being saved in the Walt Ritscher folder\\AppData\\Local. If you were to go over to that folder, there is a configuration file sitting there with the XML that's being saved. Next, we'll step out, and then we're going to save these settings. Press F11 to step in here. I'm going to write a new age value. Then the way you save to that configuration file is by calling the Save method on the auto-generated class. Then down here, we're going to read these settings again.
Now, the default age for the user will be 31. It started out as 0, and it ended up at 31. I'd like to show you one other place these settings are used in Visual Studio. That's going to be in this Windows forms application. I'm going to make this my StartUp project. Then I'm going to open up the Form designer here. Then I'm going to show you what happens when I run the application.
I'm going to press F5 to run the application. I'm going to resize the app window and then close it. Then press F5 to rerun the application the second time. Notice that the form is the same size it was before. That looks like a good use of user settings to remember the size of this window. So the Form Editor has a simple way of storing this in those user settings. I just click on the form, I go to the top of this window and choose ApplicationSettings, choose PropertyBinding, pick something called a ClientSize, and say I would like to create a new user setting, MyClientSize. The Scope is User.
Then OK. Now it's automatically generated my user settings. Now when I press F5, it'll run the application. It starts out at the default size. I make it slightly larger. When I close this, it's going to automatically save it to my UserSettings. The next time I run the application, it remembers my size. As you can see, it is very easy to work with the Settings Editor in Visual Studio. Under the covers, it's really generating a helper class on my behalf, so that I don't have to learn the complexities of the .NET configuration library.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99117 Viewers
56 Video lessons · 112398 Viewers
71 Video lessons · 81221 Viewers
131 Video lessons · 39006 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.