Click to See Complete Forum and Search --> : Config File?
Ctwizzy
March 7th, 2005, 04:05 PM
Hi,
In my application, eventually during install I would like a bunch of setting to be chosen, and that can also be changed in the program itself, for example:
path to Access DB, or maybe future change to any kind of DB.
Create Access DB from scratch?
Certain fields like in the title bar will be Organizations X application
where they can change the title bar, etc
But one of the main reasons are for flexibility. For my payroll application the user must chose if there are options a position to punch in as.
So say front, back, drive thru etc (if it was say a fast food place), now if say some other org uses my app they might need, desk, shelver etc (a library). I dont want to hardcode these in as thats counter productive. So I need a way of changing what options are available. This information would need to be kept somewhere.
Im not sure how this is typically done as Im a new programmer, so what I was thinking is a configuration file (probably XML?) which would be read upon launch to set all the settings that I would need.
Would this be the correct way to store settings? Also if this is where would I go about learning how to implement this?
Thanks!
jhammer
March 7th, 2005, 04:33 PM
XML sounds right. You can do two things:
1. Add an Application Configuration File to your project (App.Config), and then add a configuration section. This configuration file can be read automatically by .NET.
See ConfigurationSettings object and Configuration Files in the MSDN. It shows there exactly what is the correct format of the xml with example, and how to read it.
2. Create your own XML. There are lots of ways to read XML files in .NET (DataSet.ReadXML, XMLDocument object, ...).
Ctwizzy
March 7th, 2005, 04:48 PM
First way sounds much easier, Ill look into that.
Ctwizzy
March 7th, 2005, 06:22 PM
Ok so I read this article:
http://www.codeguru.com/columns/DotNet/article.php/c7987/
The custom and groups is a little over kill for me right now, so I just did the
<appSettings>
<add key="key1" value="key1svalue">
...
ok so In designer im getting this funky error which im sure must have something to do with the name of my config file.
Within the windows forms designer generated code I did this:
this.Text = ConfigurationSettings.AppSettings["ApplicationTitle"];
its crying about something or other, maybe I cant add this in the designer code, and should change it to be in the onload or something.
Anyways my projects name is Payroll.csproj
and the config gile is Payroll.config
do I need to change it to the forms name?
Thanks in advance.
Zeb
March 7th, 2005, 07:42 PM
I use
this.Text = ConfigurationSettings.AppSettings.Get("ApplicationTitle");
Norfy
March 8th, 2005, 02:12 AM
Ctwizzy,
I don't think this article is the one you want.
a) The app.config file is read-only and I thought you wanted to save values as well
b) I'm sure this file is only read at run-time so you can't use its values in the visual designer.
Try here instead for some ideas: Saving Config (http://www.codeproject.com/csharp/ReadWriteXmlIni.asp). Something new for .NET is something called Isolated Storage, but I haven't really looked into it.
Ctwizzy
March 8th, 2005, 09:31 AM
jhammer, that was the format...
Norfy, thanks ill look into that. So no saving into a config file? How do applications do it then, to update configuration settings that users do. Kind of like adobe, or firefox or anything. How does the app know to load those settings instead of the default?
Norfy
March 8th, 2005, 09:54 AM
So no saving into a config file? That's not what I said. There is no saving to the app.config file.
How do applications do it then, to update configuration settings that users do.Follow the link I posted and find out...
Ctwizzy
March 8th, 2005, 11:16 AM
You know sometimes I feel like an idiot. I spent all this money on Chris Sells Windows Forms Programming in C# and I never look in it, I always run to the forum.
I have yet to check that link out, but the book has an entie chapter on .config files, and alternatives to the read only config files such as:
Registry, Special Folders, Settings and Streams, and Isolated Storage (as you pointed out norfy).
Im going to read up on this chapter and start coding, If I run into any problems then ill come back here :)
Thanks a ton!
Ctwizzy
March 8th, 2005, 11:53 AM
Ok so Norfy quick question.
Do you advise using the dll that guy created for doing isolated storage, or should I learn to code it myself via opening the streams writing etc?
Thanks!
Krzemo
March 8th, 2005, 04:00 PM
Do you advise using the dll that guy created for doing isolated storage isolated storage.... U should be very careful with it. It is easy to accidently make it dependant on assebly version/name etc. Than user could loose all setting after upgarde :rolleyes: .
And this library.... it works (very good indeed) but it is so poorly written, not optimised, very slow. IMHO it is only starting point (but very good starting point :) )
or should I learn to code it myself via opening the streams writing etc?
It is allways beneficial! :D
Best regards,
Krzemo.
Ctwizzy
March 8th, 2005, 04:53 PM
Yes I found a Configurartion .CS file which shows how to do all the reading writing using Isolated Storage. Makes enough sense so im using it, I have noticed that it is a little slow but my boss is hampering me daily to get this over with, so if hes disappointed with speed after its up and running ill go back and fix it then.
Thanks Krzemo
Norfy
March 9th, 2005, 01:43 AM
Have you read the article yet? :)
Like the article, if you interact with your config code through an interface then you can easily change your mind at a later date and put an alternative implementation in place.
Ctwizzy
March 9th, 2005, 09:47 AM
Ill take a more thorough look later tonight thanks norfy.
MontgomeryBurns
March 9th, 2005, 10:21 AM
Hi Ctwizzy.
I often save settings in the Registry, instead of using my own files.
Note that if you want to write files on the machine, you must be sure that you have the rights to do it. So if you choose to save a file you can't do anywhere on the disk. For example you can't do it in the Application.ExecutablePath which could be write protected, if Windows was started by a user who doesn't have the administrator rights.
If you do it in the registry, you can save your settings in HKEY_CURRENT_USER\Software\Your company\Your app
Every user of the computer will have its own settings, which is quite nice, don't you think?
Ctwizzy
March 9th, 2005, 10:41 AM
With Isolated Stroage it saves it in the <user>/application data/ folder, which im pretty sure is rwx.
Registry is probably faster, but if I want to be able to add in the option to export and import a settings file, it would be easier with the XMl file, as I could just point to and overwrite the base file.
I think importing registry settings for the user would be more difficult.
Thanks anyways though.
Norfy
March 9th, 2005, 10:43 AM
Rather than the Registry you can always use a SpecialFolder. If you check MSDN you will see that they can be specific to a given user.
Zeb
March 9th, 2005, 03:40 PM
word on the street has it that the registry is on the out anyway. i can't remember where i read it, but somewhere in either msdn or some other microsoft literature, I read that they strongly encourage you not to use the registry.
if you just look at it from a deployment angle, you are far better having a config file that can be deployed with the app, or updated just by sending a new version of the config to the user.
I personally use the dataset.readxml for 90% of my configs. it's real simple to use.
ireland
November 21st, 2006, 10:17 AM
I could do with a .config file for my DLL but I see it's not the same as that for an exe. I'm interested in a runtime variable which can be edited by admin, the variable might do something like point to alternative location for other assemblies..
Are the Settings in the Project Properties of any use?
I've already implemented with a Registry setting but it has been rejected, maybe due to possible installation issues or lack or safe editability, any one know what the recommendations are for settings/config file versus Registry entry?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.