C#无法保存设置:配置文件已被其他程序更改

时间:2015-09-12 12:26:54

标签: c# exception settings

我刚刚发布了一个程序(OptiKey),该程序传递用于获取,设置和保存单个类中的用户设置的lambdas:

lambda从这个类传递(来自第143行): https://github.com/JuliusSweetland/OptiKey/blob/master/src/App.xaml.cs

到这堂课: https://github.com/JuliusSweetland/OptiKey/blob/master/src/Services/WindowManipulationService.cs

N.B。 "设置" lambda更新设置的值(例如Settings.Default.Whatever = X),然后调用Settings.Default.Save()

我无法在本地复制崩溃,但我收到来自用户的崩溃报告,其中对Save的调用抛出异常,我通过从磁盘重新加载设置来捕获并处理,然后再次尝试保存。这不起作用,重试也会抛出异常。

有人告诉我,即使用户没有使用病毒扫描程序(我认为可能会锁定user.settings文件),也会发生这种情况。

有什么想法吗?这是我遇到崩溃的最大原因,而且我不知道如何重现它!

The configuration file has been changed by another program. (C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\OptiKey.exe_Url_2dm1ifc0wqzcnmfgnsyhbssrcdx5zgbz\2.0.5.0\user.config)

<SerializableException xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ExtendedInformation>
    <HResult>-2146232828</HResult>
  </ExtendedInformation>
  <InnerException>
    <ExtendedInformation>
      <BareMessage>Failed to save settings: The configuration file has been changed by another program. (C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\OptiKey.exe_Url_2dm1ifc0wqzcnmfgnsyhbssrcdx5zgbz\2.0.5.0\user.config)</BareMessage>
      <Line>0</Line>
      <Errors>System.Configuration.ConfigurationException[]</Errors>
      <HResult>-2146232062</HResult>
    </ExtendedInformation>
    <InnerException>
      <ExtendedInformation>
        <BareMessage>The configuration file has been changed by another program.</BareMessage>
        <Filename>C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\OptiKey.exe_Url_2dm1ifc0wqzcnmfgnsyhbssrcdx5zgbz\2.0.5.0\user.config</Filename>
        <Line>0</Line>
        <Errors>System.Configuration.ConfigurationException[]</Errors>
        <HResult>-2146232062</HResult>
      </ExtendedInformation>
      <Message>The configuration file has been changed by another program. (C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\OptiKey.exe_Url_2dm1ifc0wqzcnmfgnsyhbssrcdx5zgbz\2.0.5.0\user.config)</Message>
      <Source>System.Configuration</Source>
      <StackTrace>   at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
   at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
   at System.Configuration.ClientSettingsStore.WriteSettings(String sectionName, Boolean isRoaming, IDictionary newSettings)</StackTrace>
      <TargetSite>Void SaveAs(System.String, System.Configuration.ConfigurationSaveMode, Boolean) @ System.Configuration.MgmtConfigurationRecord</TargetSite>
      <Type>System.Configuration.ConfigurationErrorsException</Type>
    </InnerException>
    <Message>Failed to save settings: The configuration file has been changed by another program. (C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\OptiKey.exe_Url_2dm1ifc0wqzcnmfgnsyhbssrcdx5zgbz\2.0.5.0\user.config)</Message>
    <Source>System</Source>
    <StackTrace>   at System.Configuration.ClientSettingsStore.WriteSettings(String sectionName, Boolean isRoaming, IDictionary newSettings)
   at System.Configuration.LocalFileSettingsProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
   at System.Configuration.SettingsBase.SaveCore()
   at System.Configuration.SettingsBase.Save()
   at System.Configuration.ApplicationSettingsBase.Save()
   at JuliusSweetland.OptiKey.App.&lt;App_OnStartup&gt;b__18(Double t) in c:\Users\Julius\Documents\GitHub\OptiKey\src\App.xaml.cs:line 251
   at JuliusSweetland.OptiKey.Services.WindowManipulationService.PersistDockThickness() in c:\Users\Julius\Documents\GitHub\OptiKey\src\Services\WindowManipulationService.cs:line 1003
   at JuliusSweetland.OptiKey.Services.WindowManipulationService.PersistSizeAndPosition() in c:\Users\Julius\Documents\GitHub\OptiKey\src\Services\WindowManipulationService.cs:line 1026
   at JuliusSweetland.OptiKey.Services.WindowManipulationService.ApplyAndPersistSizeAndPosition(Rect rect) in c:\Users\Julius\Documents\GitHub\OptiKey\src\Services\WindowManipulationService.cs:line 523</StackTrace>
    <TargetSite>Void WriteSettings(System.String, Boolean, System.Collections.IDictionary) @ System.Configuration.ClientSettingsStore</TargetSite>
    <Type>System.Configuration.ConfigurationErrorsException</Type>
  </InnerException>
  <Message>Exception has been thrown by the target of an invocation.</Message>
  <Source>mscorlib</Source>
  <StackTrace>   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)</StackTrace>
  <TargetSite>System.Object InvokeMethod(System.Object, System.Object[], System.Signature, Boolean) @ System.RuntimeMethodHandle</TargetSite>
  <Type>System.Reflection.TargetInvocationException</Type>
</SerializableException>

在代码中的其他几个地方也可以访问(获取/设置+保存)FYI设置 - 也许是同时进行,但我不认为这种情况正在发生。

1 个答案:

答案 0 :(得分:0)

我本身并没有解决这个问题。相反,我已经避免快速连续更改和保存设置。现在我更改全局静态节省类中的设置并保存在应用程序退出中。