Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
.NET applications use configuration files to change the way they behave at runtime. These configuration files are located in the same folder as the application executable. Let me show you a few configuration files inside Visual Studio. I've opened a Solution called ConfigurationFiles. It contains a ConsoleApplication, a FinancialLibrary DLL, and a WebApplication. I'm going to start by looking at the WebApplication. They commonly have a configuration file called the Web.config. It's added automatically in the project template.
And this contains settings in an XML format that might be used by this web application. Now, in web applications, you typically have at least one Web.config in the root directory of the web site, but you can also put other Web.configs in children directories. So here, you can see it's just XML. There's a section called configuration, and below that is a subsection called connectionStrings. And inside this connectionString is something with the name ApplicationServices, and then below that it has some details of how to connect to this particular SQLEXPRESS database.
This implies that in my C# or VB code, I could open this connectionString through some code. Other applications that you might create, like console applications, don't have config files. So let me look at this ConsoleApplication and see what's going on here. This ConsoleApplication has a Reference to the FinanceLib. That means when I deploy the application, I have to deploy the Console's EXE, and I also need to deploy the FinancialLib DLL. Luckily for me, in Visual Studio, when I do a compile--I'm going to come here and choose a Build Solution, I do a Show All Files and then look in the bin directory-- you'll see that it created the EXE, and then also after it's uncompiling the DLL, it made a copy here.
What I want to do is write a configuration file that allows me to change the location of this DLL, to put it in another location on my hard drive. I need a config file to tell this Common Language Runtime where to go probe for that file. So, the first thing I'm going to do is add a configuration file to this ConsoleApp. I'll right-click and choose Add > New Item. Then I'll come over here to the Search text box and type in "config". Here is the one I'm looking for: Application Config File.
Leave the name at the default, App.config, and click Add. Now, the CLR, when it's looking for a configuration file for a Windows application, expects the file to have exactly the same name as your finished executable. Now, my finished executable's name is ConsoleApp.exe. That means my config file should be called ConsoleApp.exe.config. The good news is if I compile the application right now, you'll see that there's now two copies of the config: this is one which I edit inside Visual Studio and then every time I compile it makes a copy of this and gives it the correct name.
Therefore, if I ever change the compiled version of the name later, Visual Studio will change the name of the configuration file. I need to do one more thing. I need to write some code in this configuration file that will probe in new locations. I've already got the code written. It's up here in this Assets folder. So here's what I'm going to use. I'm going to copy this piece here and then switch back to the App.config and paste that in. So, this is telling the Common Language Runtime: at runtime when you go to find an assembly, I want you to search these two extra locations: otherfiles and the bin folder. So now I'm ready.
I'll save and I'll compile. Now, I'm going to open this Folder in Windows Explorer, and what I'm going to do now is I'm going to take this Debug folder and copy it and move it somewhere else on my hardware. And so I think I'm going to move it to move it to my Desktop and paste that in. And then I'll go in here, and I'll see if this application runs. I'll double-click on this and the application runs and gets my payment value.
Okay now, let's put a folder in here, and call this one "DLLs" like that. Now, I'm going to take this FinancialLib file. I'm going to move it in there. Now when I try to run this application, it throws an unhandled exception because the Common Language Runtime is looking for that DLL and cannot find it. So, what I can do is come into this configuration file--this is just a text file--and I can create another probe.
I can say instead of looking in the bin folder, let's look in that DLLs folder. I'll save that file, and now I'm going to double-click on this ConsoleApp.exe again, and it runs successfully. So that's how I can change the runtime probe for that file. So let's review. A configuration file is loaded by the .NET runtime when the application starts up. The application reads sections of the config file to determine what settings and data to use at runtime.
You can write your own code to read the sections of the config file too. Visual Studio includes a clever tool to simplify creation of application and user-specific settings. I'll show you how this settings tool works in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99874 Viewers
56 Video lessons · 112990 Viewers
71 Video lessons · 81827 Viewers
131 Video lessons · 39263 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.