c#保存设置失败:配置文件已被其他程序更改

本文关键字:其他 程序 配置文件 保存 设置 失败 | 更新日期: 2023-09-27 18:12:23

我刚刚发布了一个程序(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

注意:"set" lambda更新设置的值(例如:Settings.Default.Whatever = X),然后调用Settings.Default.Save()

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

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

任何想法?这是我遇到的导致崩溃的最大原因,我不知道如何重现它!

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>

仅供参考,设置也可以在代码中的其他几个地方访问(get/set+save) -可能同时,但我不认为这会发生。

c#保存设置失败:配置文件已被其他程序更改

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