0

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:

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.

Datboydozy
  • 131
  • 7
  • 1
    Maybe you can read this answer first [Link](https://stackoverflow.com/a/13043569/14532718) – YHF Jun 24 '22 at 03:53
  • @YHF thanks so much that reiterates what Francis said in his answer. Thank you for that link, looks like I will be using it for my go to facts about app.config. I googled the issue but never saw that page. Thanks! – Datboydozy Jun 24 '22 at 03:59

1 Answers1

2

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

Francis G
  • 1,040
  • 1
  • 7
  • 21
  • Hi Thank you very much! this fixed my issue but I am so confused. In Visual Studio the only file that shows up is App.config and that's the file that I edited. And that is the file I thought I needed to change. Not the "CopyTDX2KlarfDiagnosticFiles.exe.config" which never shows up in my solution. I would never have figured this out. Is there anyway to make it use the one called App.config? Like how does this even work then? – Datboydozy Jun 24 '22 at 03:45
  • Without knowing C# like you do, there's no way one could know which actual config file is being ran. Not with the way Visual studio doesn't put it with that name in your solution explorer – Datboydozy Jun 24 '22 at 03:46
  • It will always change the name of the app.config on compilation. I think there is a work around to customize the name of app.config. – Francis G Jun 24 '22 at 03:50