简体   繁体   中英

Make C# Console App recognize manually edited changes in .exe.config file

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:

在这里查看设置 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 .

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://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.refreshsection?view=netframework-4.7.2

Thumbs up and mark it if it helped you!

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 value s 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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM