1

I successfully wrote a C# console application that collects .XML or .ZIP files from different locations and copies them to a single destination. Those locations are stored in the settings as User-scoped settings (for instance, "Folder01 C:\Data1\" and "Folder02:\Data2"). As you probably already know, building the project generates a [ProjectName].exe.config file in the /bin/Debug folder.

Now, the issue is that I cannot get the console app to recognize any changes that I made in the .exe.config file. Say, I want to add "Folder 03 C:\Data3\" to the settings or edit "Folder02" path to "C:\DataEdited\", the console app will still loop through the settings as initially set up in the code ("Folder01 C:\Data1\" and "Folder02 C:\Data2\").

I also noticed that the console app still runs even after deleting the .exe.config file, as if it does not rely on the file at all. I would like to make changes without having to open the project in Visual Studio and edit locally.

Is that possible?

EDIT:

In response to the request of the Settings that I created and code for getting folder paths, see image image below:

See Settings here Here is the code:

string[] acceptedExtensions = new[] { ".xml", ".zip" };
string[] settingsToSkip = new[] { "RootFolder", "ArchiveFolder" };

// Collect data
var filteredSettings = Properties.Settings.Default.Properties
    .Cast<SettingsProperty>()
    .Where(p => !settingsToSkip.Contains(p.Name));

filteredSettings collects Folder01, Folder02, Folder03 and Folder04 and I loop through those to find files with acceptedExtensions.

eddylepatio
  • 123
  • 1
  • 1
  • 11
  • How do you run your app when it doesn't recognise the config file? Launching it from visual studio might change its working folder, so that it would read setting file from somewhere else. Also, visual studio might copy your app.config file from solution directory on every launch, thus overriding all your changes. – Hirasawa Yui Dec 17 '18 at 16:39
  • 4
    Sounds like you might want to store you destination settings in a different way e.g XML file, database etc – d219 Dec 17 '18 at 16:43
  • @HirasawaYui Yes, I suspect the console app is reading the setting file from somewhere else, but where? Actually, I copy both the .exe and .exe.config files on another unit and in the same location, then just run the .exe file to collect the ZIP/XML files from the locations I set up in the app.config aka .exe.config file. It runs as expected, but when I open the .exe.config file to add a new location to collect the ZIP/XML files from, the .exe file does not recognize the change. It still runs as initially. – eddylepatio Dec 18 '18 at 04:20
  • @d219 I appreciate the idea. I just thought it could be done right with the console app... – eddylepatio Dec 18 '18 at 04:22
  • @Symon I keep both the .exe and .exe.config files in the same location, just as they are in the /bin/Debug folder when the project is built. – eddylepatio Dec 18 '18 at 04:24
  • Let me re-phrase: Within your code, does your `string fileName = ConfigurationManager[""]` (or something) actually point to the right place? – Jaskier Dec 18 '18 at 14:00
  • @Symon I don't use ConfigurationManager in my code. I just created a Settings file where I store user-scoped folder paths. When I build the project, it creates/refreshes three files: _[appName.].exe_, _[appName].exe.config_ and _[appName].pdb_. I then copy the first two files to our server, where _.exe_ will be run every 5 hours. So, I thought I could just add new folder paths to _.exe.config_ file and that _.exe_ would the change into account... – eddylepatio Dec 18 '18 at 16:00
  • From your comment- `I don't use ConfigurationManager in my code. I just created a Settings file where I store user-scoped folder paths.` sounds as though you created settings but never referenced to your settings within the program? How does your program find these settings? Feel free to edit the post with a code example of how you are accessing these settings within the code :) – Jaskier Dec 18 '18 at 16:05
  • @Symon I just edited the post. :) – eddylepatio Dec 18 '18 at 17:19
  • 1
    Reference: https://stackoverflow.com/questions/982354/where-are-the-properties-settings-default-stored --You may have an easier time with using `.exe.config` along with `ConfigurationManager` within your code. I can provide an answer (if wanted) on how to implement this – Jaskier Dec 18 '18 at 17:28
  • @Symon Please, do. – eddylepatio Dec 18 '18 at 17:37
  • 1
    User settings are, of course, stored independently for each user, not in the main application configuration file. See https://stackoverflow.com/a/469823/13087 – Joe Dec 18 '18 at 19:09

2 Answers2

2

I believe you expected this feature of c# ConfigurationManager. You might have deleted *.exe.config after your application is started. *.exe.config is not locked or needed after app starts unless you call configurationmanager.refreshsection() method.

Reloading configuration without restarting application using ConfigurationManager.RefreshSection

https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.refreshsection?view=netframework-4.7.2

Thumbs up and mark it if it helped you!

Ashokan Sivapragasam
  • 2,033
  • 2
  • 18
  • 39
1

How I have done it in my production code is that I have added to my App.config with my Visual Studios and made it have the format of:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
        <add key="AConnection" value="127.0.0.1"/>
        <add key="Folder01" value="L:\Path\To\A\Thing"/>
        <add key="Folder02" value="L:\Path\To\ASecond\Thing"/>
        <add key="Folder03" value="L:\Path\To\AThird\Thing"/>
        <add key="Folder04" value="L:\Path\To\AFourth\Thing"/>
  </appSettings>
</configuration>

Where the <add key="" value="">s are whatever you wish to name them and the values is the path to the correct file.


Assigning:

You can then assign these to variables:

string conStr = ConfiurationManager.AppSettings["AConnection"];
string strFolder1 = ConfigurationManager.AppSettings["Folder01"];
string strFolder2 = ConfigurationManager.AppSettings["Folder02"];
string strFolder3 = ConfigurationManager.AppSettings["Folder03"];
string strFolder4 = ConfigurationManager.AppSettings["Folder04"];
Jaskier
  • 1,075
  • 1
  • 10
  • 33
  • 1
    I see what you are doing here. So, if I go this route, then add the line ConfigurationManager.RefreshSection("appSettings") to refresh .exe.config before retrieving the folder paths, maybe I will get some luck? I will keep you posted. Thanks! :) – eddylepatio Dec 18 '18 at 23:17
  • 1
    It worked! Thank you so much! I'll point your input as answer. :) – eddylepatio Dec 19 '18 at 07:14
  • @eddylepatio glad I could help! Happy coding! – Jaskier Dec 19 '18 at 14:08