Tarma Tarma - 10 months ago 75
C# Question

c#: System.Configuration.ConfigurationErrorsException (empity file)

Industrial PC W7 Ultimate 64 bit

We have often problem at power down ( cut off), look like this interruption corrupt file setting we use as follow:

When the application start again, show up following message:

************** Exception Text **************

System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize --

We use setting file for reload old value in the application grapich (Windows chart framework 3.5)
Any idea Help? How to protect this writing / Reading process in system file
Are there any "FileClose" in order to protect sudden power down.

the file setting is the same:

<?xml version="1.0"?>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="WindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>

<add key="Ip_Box" value=""/>
<add key="Report_Box" value="Report"/>
<add key="MaxValSp_Box" value="1000"/>
<add key="NumSp_Box" value="10"/> <!--Numero asse mand 1 es:10 x RPM -->
<add key="NumDriveSp_Box" value="7"/> <!--Indice n. Drive Sp1:CU[0],NX[1],NX[2],NX[3],CU_L1[4],..SP[7]-->
<add key="NumSp2_Box" value="11"/> <!--Numero asse mand 2 es:10 x RPM -->
<add key="NumDriveSp2_Box" value="8"/> <!--Indice n. Drive Sp2-->
<add key="MaxValSp2_Box" value="10000"/>
<add key="NumParRead_Box" value="33"/> <!--R33 coppia attuale-->
<add key="Language" value="ita"/> <!--Language (ita-eng-deu ecc...)-->
<!-- HOME PAGE ****************************************************** -->
<add key="Table1" value="1"/> <!--1=On, 0=Off -->
<add key="Table2" value="1"/> <!--1=On, 0=Off -->
<add key="Table3" value="0"/> <!--1=On, 0=Off -->
<add key="ActivateRef." value="1"/> <!--1=On, 0=Off -->
<!-- H.S.KEY ******************************************************* -->
<add key="Hsk3" value="1"/> <!--1=On, 0=Off -->
<add key="Hsk4" value="1"/> <!--1=On, 0=Off -->
<add key="Hsk5" value="1"/> <!--1=On, 0=Off -->
<add key="Hsk6" value="1"/> <!--1=On, 0=Off -->
<add key="Hsk7" value="1"/> <!--1=On, 0=Off -->
<add key="Hsk8" value="1"/> <!--1=On, 0=Off -->
<!-- TIRANTI ******************************************************* -->
<add key="adrDB" value="601"/> <!-- DB tabella pressioni -->
<add key="adrDBW" value="80"/> <!-- DBW iniziale tabella pressioni -->
<add key="adrDBWCnt" value="22960"/> <!-- Num tot word db601 FCT DATI -->
<!-- HT8 ******************************************************* -->
<add key="Ax1" value="X"/>
<add key="Ax2" value="Y"/>
<add key="Ax3" value="Z"/>
<add key="Ax4" value="A"/>
<add key="Ax5" value="W"/>
<add key="Ax6" value="C"/>
<add key="Ax7" value="W1"/>
<add key="Ax8" value="W2"/>
<add key="Ax9" value="W3"/>
<add key="Ax10" value="B1"/>
<add key="Ax11" value="B2"/>
<add key="Ax12" value="B3"/>
<add key="Ax13" value="SP"/>
<add key="Ax14" value="A2"/> <!-- DBX126.4=1 Ax4 Visual Ax14 -->
<add key="Ax15" value="B2"/> <!-- DBX126.4=1 Ax5 Visual Ax15 -->
<add key="Ax16" value="C2"/> <!-- DBX126.4=1 Ax6 Visual Ax16 -->

<add key="Ax1Num" value="1"/> <!-- 0=Off, 1,2..ordine Visualizz -->
<add key="Ax2Num" value="2"/>
<add key="Ax3Num" value="3"/>
<add key="Ax4Num" value="4"/>
<add key="Ax5Num" value="5"/>
<add key="Ax6Num" value="6"/>
<add key="Ax7Num" value="7"/>
<add key="Ax8Num" value="8"/>
<add key="Ax9Num" value="9"/>
<add key="Ax10Num" value="10"/>
<add key="Ax11Num" value="11"/>
<add key="Ax12Num" value="12"/>
<add key="Ax13Num" value="13"/>
<add key="Ax14Num" value="14"/>
<add key="Ax15Num" value="15"/>
<add key="Ax16Num" value="16"/>

<add key="RefreshAxisTime" value="800"/> <!-- Tempo refresh quote assi -->
<add key="RefreshAxisPriority" value="2"/> <!-- 0=Low - 1=Standard - 2=hight -->
<!-- Tempo refresh quote assi -->
<!-- Tab Pressioni ***************************************************** -->
<add key="Cmp1" value="Prs I° Tirante Montante AP16 "/>
<add key="Cmp2" value="Prs II° Tirante Montante AP17 "/>
<add key="Cmp3" value="Prs III° Tirante Montante AP18 "/>
<add key="Cmp4" value="Prs Controtiro In calcolata "/>
<add key="Cmp5" value="Prs Controtiro Auto calcolata "/>
<add key="Cmp6" value="Prs Controtiro totale calcolata"/>
<add key="Cmp7" value="Prs Tir Slitta Interno AP19 "/>
<add key="Cmp8" value="Prs Tir Slitta Est. Slitta AP9 "/>
<add key="Cmp9" value="Prs feedback Proporz.CT "/>
<add key="Cmp10" value="Prs feedback Proporz.TI "/>
<add key="Cmp11" value="Prs Pattino 25 IN73 Verticale "/>
<add key="Cmp12" value="Prs Pattino 26 IN74 Verticale "/>
<add key="Cmp13" value="Prs Pattino 54 IN75 Vertical "/>
<add key="Cmp14" value="Prs Pattino_55 IN76 Verticale "/>
<add key="Cmp15" value="Prs blc leggero TRT1 "/>
<add key="Cmp16" value="Prs blc forte TRT1 "/>
<add key="Cmp17" value="Prs Meato 25 IN73 Vert.(calc. "/>
<add key="Cmp18" value="Prs Meato 26 IN74 Vert (calc) "/>
<add key="Cmp19" value="Prs Meato_54_IN75 Vert (calc) "/>
<add key="Cmp20" value="Prs Meato_55_IN76 Vert (calc) "/>
<add key="Cmp21" value="Prs blc leggero TRT2 "/>
<add key="Cmp22" value="Prs blc forte TRT2 "/>

<add key="Cmp1Num" value="1"/> <!-- 0=Off, 1,2..ordine Visualizz -->
<add key="Cmp2Num" value="2"/>
<add key="Cmp3Num" value="3"/>
<add key="Cmp4Num" value="4"/>
<add key="Cmp5Num" value="5"/>
<add key="Cmp6Num" value="6"/>
<add key="Cmp7Num" value="7"/>
<add key="Cmp8Num" value="8"/>
<add key="Cmp9Num" value="9"/>
<add key="Cmp10Num" value="10"/>
<add key="Cmp11Num" value="11"/>
<add key="Cmp12Num" value="12"/>
<add key="Cmp13Num" value="13"/>
<add key="Cmp14Num" value="14"/>
<add key="Cmp15Num" value="15"/>
<add key="Cmp16Num" value="16"/>
<add key="Cmp17Num" value="17"/>
<add key="Cmp18Num" value="18"/>
<add key="Cmp19Num" value="19"/>
<add key="Cmp20Num" value="20"/>
<add key="Cmp21Num" value="21"/>
<add key="Cmp22Num" value="22"/>
<!-- Tab Temp ***************************************************** -->
<add key="Tmp1" value="IN06 T.Ambiente "/>
<add key="Tmp2" value="IN18 T.Mand Idrostatica "/>
<add key="Tmp3" value="IN71 T.Mand Olio Cinematismi "/>
<add key="Tmp4" value="IN72 T.Mand Frigo Cinematismi "/>
<add key="Tmp5" value="IN70 T.Scarico Bussola di Raff "/>
<add key="Tmp6" value="IN21 T.I°Coppia Cuscinetti Ant "/>
<add key="Tmp7" value="IN15 T.II°Coppia Cuscinetti Ant"/>
<add key="Tmp8" value="IN22 T.Copp.Cuscinetti Centrali"/>
<add key="Tmp9" value="IN11 T.Olio Raff. Bareno "/>
<add key="Tmp10" value="IN08 T.Testa(TUP*)/II°CorpoTTWM"/>
<add key="Tmp11" value="IN69 T.Fl. Raffr. Motore MMand "/>
<add key="Tmp12" value="IN68 T.Piastra Interfaccia Tes "/>
<add key="Tmp13" value="IN99 T.Olio Mand.Idrost. Bareno"/>
<add key="Tmp14" value="IN90 T.Cusc Ant.Irrigidit Baren"/>
<add key="Tmp15" value="IN53 T.Int I° Corpo Testa/TTWM "/>
<add key="Tmp16" value="IN18 Mandata Idrostatica TRT1 "/>
<add key="Tmp17" value="IN18 Mandata Idrostatica TRT2 "/>

<add key="Tmp1Num" value="1"/> <!-- 0=Off, 1,2..ordine Visualizz -->
<add key="Tmp2Num" value="2"/>
<add key="Tmp3Num" value="3"/>
<add key="Tmp4Num" value="4"/>
<add key="Tmp5Num" value="5"/>
<add key="Tmp6Num" value="6"/>
<add key="Tmp7Num" value="7"/>
<add key="Tmp8Num" value="8"/>
<add key="Tmp9Num" value="9"/>
<add key="Tmp10Num" value="10"/>
<add key="Tmp11Num" value="11"/>
<add key="Tmp12Num" value="12"/>
<add key="Tmp13Num" value="13"/>
<add key="Tmp14Num" value="14"/>
<add key="Tmp15Num" value="15"/>
<add key="Tmp16Num" value="16"/>
<add key="Tmp17Num" value="17"/>
<!-- Grafico Temp ************************************************* -->
<add key="NumSonde" value="17"/> <!-- Num sonde Visualizzabili -->
<add key="NomiSonde" value="IN06;IN77;IN71;IN72;IN70;IN21;IN15;IN22;IN11;IN08;IN69;IN68;IN99;IN90;IN53;IN18;IN19"/>
<add key="SogliaMinX" value="0"/> <!-- Origine Base tempi -->
<add key="SogliaMaxX" value="600"/> <!-- MAX Base tempi -->
<add key="IntervalloX" value="60"/> <!-- Step Base tempi -->
<add key="SogliaMinY" value="20"/>
<add key="SogliaMaxY" value="60"/>
<add key="IntervalloY" value="2"/>
Parametri inizializzazione Mag.Ute (LibNodave)
<add key="Ip" value=""/> <!--Indirizzo IP CN -->
<add key="RefreshTime" value="1000"/> <!--Tempo di Clock (refresh) -->
<add key="TimeoutConnection" value="30000"/> <!--Tempo di riconnesione -->
<add key="textNumPocket" value="100"/> <!--Numero posti magazzino -->
Pulsanti opzione Mag.Ute

<add key="BalluffActive" value="0"/> <!--0=Off - 1=On [Pulsante Balluff] -->



<setting name="MachineTimeWorkTot" serializeAs="String">
<setting name="MachineTimeManualTot" serializeAs="String">
<setting name="MachineTimeStopTot" serializeAs="String">
<setting name="MachineTimeWorkDay" serializeAs="String">
<setting name="MachineTimeManualDay" serializeAs="String">
<setting name="MachineTimeStopDay" serializeAs="String">
<setting name="NumDay" serializeAs="String">
<value />
<setting name="ck1Green" serializeAs="String">
<setting name="ck2Green" serializeAs="String">
<setting name="ck3Green" serializeAs="String">
<setting name="ck4Green" serializeAs="String">
<setting name="ck5Green" serializeAs="String">
<setting name="ck6Green" serializeAs="String">
<setting name="ck7Green" serializeAs="String">
<setting name="ck8Green" serializeAs="String">
<setting name="ck9Green" serializeAs="String">
<setting name="ck10Green" serializeAs="String">
<setting name="ck11Green" serializeAs="String">
<setting name="ck12Green" serializeAs="String">
<setting name="ckMoGreen" serializeAs="String">
<setting name="ckTuGreen" serializeAs="String">
<setting name="ckWeGreen" serializeAs="String">
<setting name="ckThGreen" serializeAs="String">
<setting name="ckFrGreen" serializeAs="String">
<setting name="ckSaGreen" serializeAs="String">
<setting name="ckSuGreen" serializeAs="String">
<setting name="tbWeektimeGreen" serializeAs="String">
<setting name="T1Green" serializeAs="String">
<setting name="T2Green" serializeAs="String">
<setting name="T3Green" serializeAs="String">
<setting name="T4Green" serializeAs="String">
<setting name="T5Green" serializeAs="String">
<setting name="T6Green" serializeAs="String">
<setting name="T7Green" serializeAs="String">
<setting name="T8Green" serializeAs="String">

<supportedRuntime version="v2.0.50727"/>

<generatePublisherEvidence enabled="false"/>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

I save the configuration file in the timer.
I use this code for save: (example: Properties.Settings.Default.MachineTimeWorkTot = timeMachineWork.ActualVal; Properties.Settings.Default.Save())

Thanks a lot

Answer Source

In general ApplicationSettingsBase does not handle power failure scenarios.

My first and most important suggestion is to deal with this scenario using an UPS. Take the pill and accept that whatever you do you won't have a 100% reliable software solution (at best the problem may be mitigated and corruption avoided with a well-designed software implementation and/or with file system support.)

Now I assume you went out to buy an UPS and you want to make your software more reliable. The things you have to do are these:

  • Make a copy of your original unchanged configuration when program startups. It is proved to be correct and it can be used to restore configuration if required (changes from last run will be lost but at least you will still have something.)
  • If configuration fails to load then restore from backup (if present) and try to load again.

Now if you keep using ApplicationSettingsBase then you may stop here. You may want to handle ConfigurationLoading event but it's just too hacky and I'd not feel to suggest it. Alternatively you can use custom ConfigurationSection and try to save your data in a little bit more reliable way:

1) Save your settings to a new file Your App.exe.config.new.

2) Make a copy of existing settings to Your App.exe.config.bak.

3) Replace Your App.exe.config with Your App.exe.config.new.

4) Optionally delete both .bak and .new files (unless you want to keep them for recovery...somehow).

Note that point 2, 3 and partially 4 are handled by System.IO.File.Replace() function. Do not try to re-implement it unless you're sure to make it correct and add some value.

All this said...you're saving application state inside configuration files. It's not, in my opinion, the right usage. They were not designed for this purpose. You may consider to use a better and more reliable storage for your state: what about a local database? It comes with built-in reliable I/O (better than what we may expect to do by hand. Not even to mention the other advantages (possibility to query values, intrinsically shared between processes, optionally journaled, easy to update/deploy/merge and so on...)