Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2007, 02:20 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using Application Configuration Files

Hello all. I am building an application that requires the storage of certain settings to be accessed globally from several classes. For example, one such setting is the path of a web browser used to open web pages. I am guessing I would use an Application Configuration File for storing this information, but how do I go about using one? Thanks.

If only computers could write programs themselves...
__________________
If only computers could write programs themselves...
 
Old April 15th, 2007, 06:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to add one to your project. This should be straightforward if you are using visual studio or some variant. The file will be added as "app.config" when you compile the application it will get copied to the runtime directory as "myapplication.exe.config".

-Peter
 
Old April 15th, 2007, 07:57 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, that much I already understand. My question is how do I reference it in code, say to get or change the value of a specific setting for the program (such as the path of a web browser).

If only computers could write programs themselves...
 
Old April 15th, 2007, 10:43 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Usually with something like this:

appsettingvalue = ConfigurationManager.AppSettings["myappsettingkey"];

I'm not sure about writing values. Usually these are used in a read only fashion because they are for application configuration, not runtime preference storage. If you are looking to create some kind of a preferences storage mechanism you might want to consider something different, such as a preferences class that you store as a serialized XML file.

-Peter
 
Old April 16th, 2007, 09:10 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, here is what I have done. I have created an XML file called settings.xml. I have set up VS to put a copy in the program directory when the program is built (and I am assuming when installed). The XML file stores two settings, a string variable, and the path to a web browser.

I can open the file, read to it, and make changes. When I change only the value of the string variable, I am able to save the XML file without issue. However, once I use an openFileDialog to select the web browser path, and go to save the changes to the XML file (using doc.Save("settings.xml"); ), I get an error because it is looking for settings.xml in the folder I selected in the openFileDialog. My question is how to write a doc.Save method that uses a path which is relative the the program's directory, to avoid this error. Thanks.

If only computers could write programs themselves...
 
Old April 16th, 2007, 09:21 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This doesn't make sense. What are you doing with the return value from the open file dialog? I don't understand how that can be affecting a static line of code that saves the xml doc. Specifying a filename without a directory should imply the runtime directory.

-Peter
 
Old April 17th, 2007, 06:04 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's what I though, so I must be doing something wrong. Here is some code:

================================================== ======================
1) A button, named btnBrowse, is click on by the user to start the openFileDialog to get the file path. To file name is written in a textbox, txtWebBrowserPath

//Allow user to browse computer for web browser
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Program Files|*.exe";
            openFileDialog1.Title = "Select Web Prowser Path";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Fill in textbox with web browser path
                txtWebBrowserPath.Text = openFileDialog1.FileName.ToString();
            }


        }

================================================== ====================
There are also five radio buttons on this "options window". Whenever the user clicks on, the program sets a string variable, selectedDictionary, to a value which corresponds to the radio button that the user clicked. When the Save Preferences (btnSave) button is clicked, here is what the program does. This would also be where the error takes place, when the program saves the XML file.

//Used to store value from switch statement
            string saveDictID = "defaultValueErrorCatch"; //initialized in case some error occurred

            //Determine which radio button is selected
            switch (selectedDictionary)
            {
                case "DictID1" :
                    saveDictID = "dictID1";
                    break;

                case "DictID2" :
                    saveDictID = "dictID2";
                    break;

                case "DictID3" :
                    saveDictID = "dictID3";
                    break;

                case "DictID4" :
                    saveDictID = "dictID4";
                    break;

                case "DictID5" :
                    saveDictID = "dictID5";
                    break;

                default:
                    MessageBox.Show("An unknown error occurred while determining radio button selection.");
                    break;
            }

            //Open XML file, write changes
            XmlDocument saveDoc = new XmlDocument();
            saveDoc.Load("settings.xml");
            XmlNamespaceManager savensmgr = new XmlNamespaceManager(saveDoc.NameTable);
            savensmgr.AddNamespace("av", "autovocab");
            XmlNode saveNodeDictionary = saveDoc.SelectSingleNode("/av:Settings/av:Dictionary", savensmgr);
            XmlNode saveNodeBrowser = saveDoc.SelectSingleNode("/av:Settings/av:WebBrowserPath", savensmgr);
            saveNodeDictionary.InnerText = saveDictID;
            saveNodeBrowser.InnerText = txtWebBrowserPath.Text;
            saveDoc.Save("settings.xml");
        }

================================================== =====================

Hopefully this helps. If not I may just give up on allowing the user to select the path to their web browser of choice, and use System.Diagnostics.Process.Start()

If only computers could write programs themselves...
 
Old April 17th, 2007, 06:21 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

First, if computers wrote programs themselves, I would be out of a job. Thank god they don't. Secondly, why are you trying to use a System.Windows.Forms class in a web project?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 17th, 2007, 11:52 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why do I use System.Windows.Forms? To tell you the truth, I am newb, and so I'm not 100% sure why that is there. Anyway, I think I may need some more practice working with XML files before I finish this program!

If only computers could write programs themselves...
 
Old April 18th, 2007, 06:23 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

This code here:
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Program Files|*.exe";
            openFileDialog1.Title = "Select Web Prowser Path";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Fill in textbox with web browser path
                txtWebBrowserPath.Text = openFileDialog1.FileName.ToString();
            }

This is something I would expect to see in a windows based application not a web based application. My whole point is: if i cut and paste that above code and place it into VS2005 or VWD I recieve this error when I run the app: It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

The openFileDialog is designed to be used in a windows environment (Which is why it is in the System.Windows.Forms namespace) and, by and large, it is bad practice to try and use this in a web application because this call would actually try and spawn the box on the SERVER. (How you have this running to a point where the box displays is beyond me).

In any case here are reference posts to somewhat validate my above comments.
http://forums.asp.net/thread/1022529.aspx
http://www.thescripts.com/forum/thread333863.html

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Configuration Override Files Jeff Mason Visual Studio 2005 1 March 14th, 2007 08:52 AM
Configuration files and performance ernestlambert BOOK: ASP.NET Website Programming Problem-Design-Solution 1 April 21st, 2005 11:23 AM
Configuration files hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 March 23rd, 2005 09:13 AM
Using multiple configuration files Johan VS.NET 2002/2003 0 November 10th, 2003 12:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.