c#: System.Configuration.ConfigurationErrorsException (empit

本文关键字:empit ConfigurationErrorsException Configuration System | 更新日期: 2023-09-27 18:11:25

应用程序:工业PC W7 Ultimate 64位

问题:我们经常在断电(切断(时遇到问题,看起来像是这种中断损坏的文件设置,我们使用如下:属性。设置。默认值。

当应用程序再次启动时,显示以下消息:

**************异常文本**************
System.Configuration.ConfigurationErrorsException:配置系统未能初始化--

我们使用设置文件在应用程序grapich(Windows图表框架3.5(中重新加载旧值有什么主意吗?如何保护系统文件中的写入/读取过程是否有"文件关闭"以保护突然断电。

文件设置相同:

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

<appSettings>
    <add key="Ip_Box" value="192.168.214.241"/>
    <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="192.168.214.1"/>           <!--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] -->
    <!-- 
    ********************************************************************************
    -->
  </appSettings>


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

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

我将配置文件保存在计时器中。我使用以下代码进行保存:(例如:Properties.Settings.Default.MachineTimeWorkTot=timeMachineWork.ActualVal;Properties.Setting.Default.save(((

非常感谢

c#: System.Configuration.ConfigurationErrorsException (empit

一般情况下,ApplicationSettingsBase不处理电源故障情况。

我的第一个也是最重要的建议是使用UPS来处理这种情况。服用避孕药,接受无论你做什么,都不会有100%可靠的软件解决方案(通过精心设计的软件实现和/或文件系统支持,问题最多可以得到缓解并避免损坏。(

现在我想你是去买UPS的,你想让你的软件更可靠。你必须做的事情是:

  • 在程序启动时,复制原始的未更改配置。它被证明是正确的,如果需要,它可以用于恢复配置(上次运行的更改将丢失,但至少您仍有东西。(
  • 如果配置无法加载,则从备份中恢复(如果存在(,然后尝试再次加载

现在,如果您继续使用ApplicationSettingsBase,那么您可以到此为止。你可能想处理ConfigurationLoading事件,但它太容易了,我不建议你这样做。或者,你可以使用自定义ConfigurationSection,并尝试以更可靠的方式保存数据:

1( 将您的设置保存到新文件Your App.exe.config.new中。

2( 将现有设置复制到Your App.exe.config.bak

3( 将Your App.exe.config替换为Your App.exe.config.new

4( 可以选择同时删除.bak.new文件(除非您想保留它们以备恢复…以某种方式(。

注意,点2、3和部分4由System.IO.File.Replace()函数处理。除非你确信它是正确的并增加了一些价值,否则不要试图重新实现它。


所有这些都说明。。。您正在将应用程序状态保存在配置文件中。在我看来,这不是正确的用法。它们不是为这个目的而设计的。您可以考虑为您的州使用更好、更可靠的存储:本地数据库怎么样?它具有内置的可靠I/O(比我们手动所期望的要好。更不用说其他优势(查询值的可能性,进程之间本质上共享,可以选择日志记录,易于更新/部署/合并等等…(

                            **WORKAROUND WITH EXTERNAL SIGNAL**

早上好。。。目前我们通过变通方法解决:

  • 通过PLC位捕获UPS信号(使用电池工作(并与pc共享此信息;

  • 将UPS信息放入计时器中,如果为true,则停止计时器并使Application.Exit((;

  • 移动Form_Closed((事件中的设置备份,使setting.default.save((base.dispose(true(

我试图捕捉计算机关闭事件,但它很复杂。此解决方案是满电的,但更好的解决方案是使用pc关机事件而不是UPS位。

感谢