Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

App.config Not using Values outside of Visual Studio

I’m new to C#. In the past projects I have worked on, I inherited code where the user already created some default config file system to allow a user to be able to input their own settings outside of the developer environment. Where all they need is the exe and config file which they can edit.

I am writing a simple file copier application and it dawned on me that the two projects I’ve worked on the users went out of their way to write their own way to use config files. To save time, I looked and saw that Visual Studio gives you a default app.config file to use. So I tried to use it.

My issue:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have this bit of code:

public static void GetBaseSoureDirSubFolders()
{
    BaseSourcePath = ConfigurationManager.AppSettings.Get("BaseSourcePath");
    Console.WriteLine($"Base Dir Is: { BaseSourcePath}");
    DirectoryInfo dirInfo = new DirectoryInfo(BaseSourcePath);
    BaseSubFolders = dirInfo.GetDirectories();
}

When I run the code inside visual studio with my appconfig file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <appSettings>
      <add key="BaseSourcePath" value="C:\TDX2KlarfOutputs\"/>
      <add key="share" value="\\cdserver.com\share\"/>
      <add key="UserCount" value="2"/>
    </appSettings>
</configuration>

it does return the value I have inside the file that I wrote in Visual studio.

The problem happens when I take the exe file and the app.config file and put it in the location I want to run this code. When I run the code, it gives me C:\TDX2KlarfOutputs\ instead of the new value I put inside app.config for that BaseSourcePath. It still uses the one I set in Visual studio.

THings I have tried:
I have set it to app.config properties to Build Action =content and CopytoOutputDirector = copy if newer.

I have tried using this in my main() function :

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @".\App.config");

But this doesn’t work. I am very new to C# so I’m probably not using the right terminology to describe the issue. The issue is, how can I make my console app, use values set by user on their copy of app.config file outside of Visual studio’s values?

I have tried to google this but maybe due to my lack of experience I cannot find a question that answers my issue.

>Solution :

I think you are not using the compiled app.config but instead the uncompiled file.
If your program (.exe) name is copier, it should create a copier.app.config file.
Try to edit that.

Hope my answer helps you

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading