Question

I'm having some trouble with a current project. I have a Solution with 2 separate projects in it. The first is my Project that I am publishing, which has a web.config file. The second is a data access project, which only contains .cs files, and has an app.config file, as well as a Properties folder containing a Settings.settings file.

Like so:

Solution
    DataProject
        app.config
        Settings.settings
        Code.cs

    PublishProject
        Code Files
        Web.config

My Problem is that I want to be able to edit the app.config file settings after the solution has been published, but the app.config file gets rolled into a DataProject.dll that sits in the bin folder in PublishProject. Because of this I cannot access the app.config file to edit it post-publishing.

I have tried to setup the app.config file as a linked file that just points to the Web.config file in the other project, but the issue there was that the Settings file does not get updated with the correct values from the app.config file unless you open the settings file in Visual Studio and allow it to reload. The values in the settings file are the ones that are being used throughout the application.

Right now are only workaround is to change out the configuration and re-publish the project.

Was it helpful?

Solution

In case anyone else runs into this problem, here is the workaround that I was able to use:

Create an xml file in the PublishProject, and convert the data in the app.config into individual xml tags like so:

<configuration>    
    <setting name="debug" serializeAs="String">
              <value>False</value>
    </setting>
</configuration>

becomes

<Properties>
    <debug>True</debug>
</Properties>

Once you do this, you can call the following in your code, instead of using the value from the Settings.settings file.

XmlDocument doc = new XmlDocument();
String appPath = HttpContext.Current.Server.MapPath("~").ToString() + @"/App_Data/Properties.xml";
doc.Load(appPath);
debug = Convert.ToBoolean(doc.SelectSingleNode("Properties/debug").InnerText.ToString());

Note: that I put it in the App_Data folder of the finished project because that folder is not accessible via a browser by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top