C# application configuration is corrupted RRS feed

  • Question

  • Dear All,

    I have a Windows application that is corrupted many times because of the file *.exe.config is corrupted. I use this file to store app settings. This file is read/write many times when the user uses the software.
    Sometimes the app cannot start, in this case, I can see the real error in Event Viewer. All the cases are because of errors related to this file. I must delete *.exe.config to solve it but deleting will lose settings.

    So can anyone show me:
    1. What is the root reason why the file *.exe.config is usually corrupted?
    2. Should I avoid using this file? Instead, I can store my settings in another file?
    3. is there a way we can know and fix this type of error?


    Wednesday, August 5, 2020 3:27 AM

All replies

  • Don't use setting via project properties but instead add them and read write manually in say a static class.

    Simple example

    <?xml version="1.0" encoding="utf-8" ?>
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
        <add key="LinesToSplit" value="1000" />

    Read/write, requires using System.Configuration;

    public class ApplicationSettings
    	public static int GetLineSplit()
    			return Convert.ToInt32(ConfigurationManager.AppSettings["LinesToSplit"]);
    		catch (Exception e)
    			return 2000;
    	public static void SetLineSplit(string value)  
    			var applicationDirectoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    			// ReSharper disable once AssignNullToNotNullAttribute
    			var configFile = Path.Combine(applicationDirectoryName, $"{Assembly.GetExecutingAssembly().GetName().Name}.exe.config");
    			var configFileMap = new ExeConfigurationFileMap {ExeConfigFilename = configFile};
    			var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    			config.AppSettings.Settings["LinesToSplit"].Value = value;
    		catch (Exception e)

    These belong in the same class above, I broken them out so you realize these are needed after a set operation else the changes made don't take affect until the app restarts which is not what we would want.

    /// <summary>
    /// Must be called whenever values are changed as .NET only reads app settings once per session 
    /// </summary>
    public static void Reload()
    /// <summary>
    /// Start fresh reading settings
    /// </summary>
    public static void Fetch()
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    Now if the above gets corrupt there is something going on that I can'd reply too as the information you provided does not indicate what else might be an issue. The above I have been using for years without a hitch for several windows forms utility apps.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Wednesday, August 5, 2020 3:41 AM
  • Did you compare the original good .exe.config file with the problematic one to confirm the corruption and its nature? Show some details.

    Alternatively, you can define the parameters in Settings tab of Project Properties. Then you will have something like this:

    // reading:

    int current_value = Properties.Settings.Default.MySetting1;

    // writting:

    Properties.Settings.Default.MySetting1 = new_value;

    Properties.Settings.Default.Save( );

    Wednesday, August 5, 2020 5:55 AM
  • Hi Lehuynh,

    Thank you for posting here.

    The *.exe.config file is not essentially different from the ordinary configuration file. As long as you can modify the code, you can avoid using it and store the settings in other files.

    But this is not the key to the problem, the code that will cause the *.exe.config file to be damaged will also cause the new configuration file to be damaged.

    Only by finding the damaged part of the file and finding the code that caused the damage can we solve this problem.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 5, 2020 6:32 AM
  • The exe.config should not be edited by the program ever. This is a bad design and won't work unless the installer modifying the security permissions where the app is installed to because by default users cannot write to the default directory for programs.

    You should either use a custom settings file (like most apps) or if your settings are simple use user settings as defined in MSDN. These are stored per user. If you want to share settings between all users then store the settings in Public Documents or equivalent.

    By using a separate file you help protect yourself from corruption. While you most likely have a bug in your code that is causing the corruption there is no way to recover if you mess up the exe.config because the runtime cannot start the app. However by using a separate file (because exe.config doesn't change) then your app can start up normally, determine that the user configuration is invalid and recover from it. Furthermore, you can build in backup support as well. One of the big issues with updating a file in place is if something goes wrong you have a partially modified file. It is corrupt. Most apps will create a temporary file and make the changes their. Then rename the old file as a backup and rename the temporary file to the new file. This protects from the most common corruption scenarios.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 5, 2020 2:50 PM
  • The config file seems to be used as some sort of data store.

    Config should contain only setup information to get the app started, not data used to store processed information that the app is "processing"

    This should not be, a separate file should be used to store the data.

    Tuesday, August 18, 2020 10:49 AM