CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Config File?

  1. #1
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Config File?

    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!
    Will Rate Posts for help!

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Config File?

    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, ...).

  3. #3
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    First way sounds much easier, Ill look into that.
    Will Rate Posts for help!

  4. #4
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    Ok so I read this article:

    http://www.codeguru.com/columns/DotN...cle.php/c7987/

    The custom and groups is a little over kill for me right now, so I just did the
    Code:
     <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:
    Code:
     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.
    Will Rate Posts for help!

  5. #5
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576

    Re: Config File?

    I use
    Code:
    this.Text = ConfigurationSettings.AppSettings.Get("ApplicationTitle");

  6. #6
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Config File?

    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. Something new for .NET is something called Isolated Storage, but I haven't really looked into it.
    Useful? Then click on (Rate This Post) at the top of this post.

  7. #7
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    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?
    Will Rate Posts for help!

  8. #8
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Config File?

    Quote Originally Posted by Ctwizzy
    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...
    Useful? Then click on (Rate This Post) at the top of this post.

  9. #9
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    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!
    Will Rate Posts for help!

  10. #10
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    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!
    Will Rate Posts for help!

  11. #11
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Config File?

    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 .
    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!

    Best regards,
    Krzemo.

  12. #12
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    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
    Will Rate Posts for help!

  13. #13
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Config File?

    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.
    Useful? Then click on (Rate This Post) at the top of this post.

  14. #14
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    204

    Re: Config File?

    Ill take a more thorough look later tonight thanks norfy.
    Will Rate Posts for help!

  15. #15
    Join Date
    Jul 2003
    Location
    Springfield
    Posts
    190

    Re: Config File?

    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?
    Mr. Burns

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured