[go: up one dir, main page]

0% found this document useful (0 votes)
28 views5 pages

Notes 123

The document discusses using configuration files in .NET applications. It explains that configuration files allow storing settings like connection strings externally so code doesn't need recompiling when settings change. It provides an example app.config file format and code to read settings from it using the ConfigurationSettings class.

Uploaded by

Vicky Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Notes 123

The document discusses using configuration files in .NET applications. It explains that configuration files allow storing settings like connection strings externally so code doesn't need recompiling when settings change. It provides an example app.config file format and code to read settings from it using the ConfigurationSettings class.

Uploaded by

Vicky Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Displaying Simple MessageBox

About the sample application This application has 1 textbox and a button. If you type anything in the textbox and press the button, it will display the same text you typed in a MessageBox. Download and unzip the sample application from this page. After you unzip, you will get several files. Some of the important files are : 1. Chapter1.csproj - This is the C# project file. Double click this file to open the project. 2. Form1.cs - This is the file for the Form1 in teh sample application. After you open the project, see the 'Solution Explorer' bar on the right side of the Visual Studio. You can click on that to expand it, which will display the list of all files in the project. (Or, you can go to the menu 'View > Solution Explorer' to open the solution explorer. Click on 'Form1.cs' in the solution explorer to open the Form. Now you can see the Form in 'design mode'. Right click on the 'Form1.cs' in the solution explorer and choose 'View Code' to see the source code behind this Form (Or, press F7 from the Form to see the code). Let us analyze the code: The code has lot of Visual Studio generated code. For timebeing ignore all those code and scroll down to the event handler code in the bottom of the file.

System.Windows.Forms.MessageBox.Show( "You typed : " + txtMessage.Text );

DialogResult result = MessageBox.Show( "Did you like this application ?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if ( result == DialogResult.Yes ) MessageBox.Show("You selected YES"); else MessageBox.Show("You selected NO");

The first line will simply display a messagebox and show the text typed by the user. 'MessageBox' is a class inside the namespace 'System.Windows.Forms'. We have a statement 'using System.Windows.Forms' on top of the source code. This is like a source code. This will help us avoid

typing 'System.Windows.Forms.' everytime when we want to use a class inside this namespace. The next few lines of code do a bit more. Here we are displaying a message box, with some additional attributes. The second optional parameter of the 'Show' method is the 'caption' for the messagebox. The third parameter tells that the messagebox will have two buttons - YES and NO. You have several other options like MessageBoxButtons.OK, MessageBoxButtons.OKCancel etc. And, the last parameter says 'display a question Icon in the messagebox'. There are many other types of icons you can display. And another important part is, the MessageBox returns a value when the user press a button to close the messagebox. Our MessageBox asks a question 'Did you like this application ?" and gives two options to the user - YES or NO. The return value is of type 'DialogResult' and is assigned to the variable 'result'. We are checking the value of the result and depending on whether user pressed the YES button or NO button, appropriate message will be displayed.

Application Configuration Files


.NET gives an easy way to store configuration information in a ApplicationConfiguration File. In the simple implementation, you can store information as Key-Value pairs. For example, consider a case where you have to use a DataSource in your application. If you hardcore the DataSource information in your code, you will have a bad time when you have to change this datasource. You have to change your source code and re-compile it. This won't work everytime you give your product to different customers or when you run your application in different machines! In earlier days, programmers used to store this information in special files called ".ini" files or in system registry. The application can read the information from the .ini file or registry and no need to re-compile the code when the values are changed in .ini file or registry. But this is a pain most of the time. It is not fun opening the registry, locate your entries and make appropriate changes. It is quite possible that you may mess up with some important entries in the registry and make your system not running any more. In fact, in secured systems, administrator may deny access to the registry and users will not have the choice to edit the registry at all. .NET gives you a simple and easy solution for this problem - the ApplicationConfiguration File. Each

application can have a configuration file, which is actually an XML file. You can use any text editor (including notepad) to open the configuration file and change the values. The application will load the values from this configuration file and you do not have to change your source code everytime you change your DataSource or any other information stored in configuration file. app.config for Windows applications Windows applications in VS.NET uses the name 'app.config' by default for the configuration file. This will not be automatically created when you create a Windows application. If you need a configuration file for your application, open your project in VS.NET, go to the 'Solution Explorer' and right click on the project name. Choose Add > Add new item from the menu and select 'Application Configuration file' from the list of choices. This will create an app.config file for you in the application root. By default, the app.config file will have the following content:

<?xml version="1.0" encoding="utf-8" ?> <configuration> </configuration>

To store values in configuration file, you can create xml elements in the format

<add key="MyKey" value="MyValue" />

See the sample config entries below:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings>

<add key="DatabasePath" value="c:\\projects\data\spider.mdb" /> <add key="SupportEmail" value="webmaster6@dotnetspider.com" /> </appSettings> </configuration>

And to read from this config file, just use the following code in your application:

string dbPath = System.Configuration.ConfigurationSettings.AppSettings["D atabasePath"]; string email = System.Configuration.ConfigurationSettings.AppSettings["S upportEmail"];


ConfigurationSettings is the class
used to access the contents of the configuration file. Since this class is part of the namespace name

System.Configuration,

we have to use the fully qualified

System.Configuration.ConfigurationSettings. As a shortcut, you can use the using directive


on top of the file like below:

using System.Configuration;
If you have the above directive on top of the file, then you can directly use the class

ConfigurationSettings.

string dbPath = ConfigurationSettings.AppSettings["DatabasePath"];

string email = ConfigurationSettings.AppSettings["SupportEmail"];

In VB.NET, you have to use "( ... )" instead of the "[ ... ]", as shown below:

Dim dbPath as String = ConfigurationSettings.AppSettings("DatabasePath") Dim email as String = ConfigurationSettings.AppSettings("SupportEmail")

You might also like