Can I stop Visual Studio 2008 from overwriting my application configuration file?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Can I stop Visual Studio 2008 from overwriting my application configuration file?

  1. #1
    Join Date
    Aug 2006
    Posts
    132

    Can I stop Visual Studio 2008 from overwriting my application configuration file?

    A colleague wrote an application named "cranesimulator" in C# and Visual Studio 2008 that contains settings in a configuration file. There is a file in the project named "app.config". When the project gets built, that file gets copied into cransesimulator.exe.config in the project output directory.

    If I want to change a simulation parameter such as the time rate (say from 1 real second per 1 simulated minute to 1 real second per 10 simulated seconds), I can change cranesimulator.exe.config. But if I have to rebuild the cranesimulator project, app.config gets copied over the top of the exe.config file, and my changes get lost. Is there any way to stop VS 2008 from replacing the exe.config file?

    Or is there some way I can change what file the application is looking for? ConfigurationManager.OpenMappedExeConfiguration() seems to do what I want, if I understand the documentation correctly.

    Thank you very much.

    RobR

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,365

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    The simplest way is to change the app.config file located in the project's directory. If you make changes to this file, it will get copied over and renamed to myproject.exe.config.

  3. #3
    Join Date
    Aug 2006
    Posts
    132

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    Thank you for that reply.

    Unfortunately, that is not going to help. I think that the use of app.config in this application was a poor choice. The problem is that the configuration parameters are likely to change as various tests are performed, and the designer should have recognized that fact. That leads to two completely different test scenarios. If the program is run in debug mode from inside Visual Studio, then I have to remember to set my parameters in app.config. If I am finished with debugging for the moment and am running tests from a command line, I have to remember to set my parameters in myprogram.exe.config. And as soon as I go back into debugging mode, the parameters I had set in myprogram.exe.config will get lost.

    And then there's the difficulty of working with System.Configuration. I found I needed another application to support the testing I'm doing. I tried to use the same configuration file, but I couldn't get it to work. I wanted to use System.Configuration.OpenMappedConfigurationFile to bring in the original application's configuration, but it didn't work. I spent the better part of a day on it. Finally, I stripped out reliance on app.config and System.Configuration from the original application and replaced it with System.Xml.Linq, and the whole problem went away.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,365

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    Ok. I believe there is a basic misunderstanding for what the app.config file is used for. It's for read-only settings that are read during program startup.

    If you need some other functionality (like r/w settings), you'll need to use something other than the default app.config mechanism.

  5. #5
    Join Date
    Aug 2006
    Posts
    132

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    There are read-only settings, but they need to be changed for different test configurations. App.config and its automatic overwriting of myprogram.app.config makes it unsuitable for that purpose.

  6. #6
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    40

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    Program configuration settings (preferences if you will) are best stored in the database that the application uses (if it is using one). Otherwise, have the application create and manage it's own configuration file or use the registry - though I'd recommend the former.

    -Max

  7. #7
    Join Date
    Jul 2012
    Posts
    90

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    See "http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx". Read the parts pertaining to user settings and user.config. Basically, the settings in the app.config should be read-only default settings. If you have settings that you need to change at run-time on a per user basis, they go into a user.config file associated with that user's local profile.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,365

    Re: Can I stop Visual Studio 2008 from overwriting my application configuration file?

    Quote Originally Posted by RobR View Post
    App.config and its automatic overwriting of myprogram.app.config makes it unsuitable for that purpose.
    Yes, it simply isn't designed to work that way. During testing, if you need to change the settings, do it on the fly to the *.exe.config file before launching the app and running your tests. Or do something else that fits your needs.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center