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

C#无法保存设置:配置文件已被其他程序更改,c#,exception,settings,C#,Exception,Settings,我刚刚发布了一个程序(OptiKey),它通过lambdas,用于在单个类中获取、设置和保存用户设置: lambda从此类传递(从第143行): 对于这一类: 注意,“set”lambda更新设置的值(例如Settings.Default.Whatever=X),然后调用Settings.Default.Save() 我无法在本地复制崩溃,但我收到了来自用户的崩溃报告,其中对Save的调用引发了异常,我通过从磁盘重新加载设置,然后再次尝试保存来捕获和处理异常。这不起作用,重试也会引发异常 有

我刚刚发布了一个程序(OptiKey),它通过lambdas,用于在单个类中获取、设置和保存用户设置:

lambda从此类传递(从第143行):

对于这一类:

注意,“set”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>
配置文件已被其他程序更改。(C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\optkey.exe\u Url\u 2dm1ifc0wqzcnmfgnsyhbsrcdx5zgbz\2.0.5.0\user.config)
-2146232828
未能保存设置:配置文件已被其他程序更改。(C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\optkey.exe\u Url\u 2dm1ifc0wqzcnmfgnsyhbsrcdx5zgbz\2.0.5.0\user.config)
0
System.Configuration.ConfigurationException[]
-2146232062
配置文件已被其他程序更改。
C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\optkey.exe\u Url\u 2dm1ifc0wqzcnmfgnsyhbsrcdx5zgbz\2.0.5.0\user.config
0
System.Configuration.ConfigurationException[]
-2146232062
配置文件已被其他程序更改。(C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\optkey.exe\u Url\u 2dm1ifc0wqzcnmfgnsyhbsrcdx5zgbz\2.0.5.0\user.config)
系统配置
位于System.Configuration.MgmtConfigurationRecord.SaveAs(字符串文件名、ConfigurationSaveMode saveMode、布尔值forceUpdateAll)
在System.Configuration.Configuration.SaveAsImpl(字符串文件名、ConfigurationSaveMode保存模式、布尔值forceSaveAll)
在System.Configuration.ClientSettingsStore.WriteSettings(String sectionName,Boolean isRoaming,IDictionary newSettings)中
Void SaveAs(System.String,System.Configuration.ConfigurationSaveMode,布尔值)@System.Configuration.MgmtConfigurationRecord
System.Configuration.ConfigurationErrorsException
未能保存设置:配置文件已被其他程序更改。(C:\Users\Charlie Sheen\AppData\Roaming\JuliusSweetland\optkey.exe\u Url\u 2dm1ifc0wqzcnmfgnsyhbsrcdx5zgbz\2.0.5.0\user.config)
系统
在System.Configuration.ClientSettingsStore.WriteSettings(String sectionName,Boolean isRoaming,IDictionary newSettings)中
位于System.Configuration.LocalFileSettingsProvider.SetPropertyValues(SettingsContext上下文,SettingsPropertyValueCollection值)
在System.Configuration.SettingsBase.SaveCore()中
在System.Configuration.SettingsBase.Save()中
在System.Configuration.ApplicationSettingsBase.Save()中
在c:\Users\Julius\Documents\GitHub\OptiKey\src\App.xaml.cs中的JuliusSweetland.OptiKey.App.App_OnStartupb_u18(双t)处:第251行
在c:\Users\Julius\Documents\GitHub\OptiKey\src\Services\WindowManufactionService.cs中的JuliusWeetland.OptiKey.Services.WindowManufactionService.PersistDockHickness()处:第1003行
在c:\Users\Julius\Documents\GitHub\OptiKey\src\Services\WindowManufactionService.cs中的JuliusWeetland.OptiKey.Services.WindowManufactionService.PersistSizeAndPosition()中
在c:\Users\Julius\Documents\GitHub\OptiKey\src\Services\WindowManipulationService.ApplyAndPersistSizeAndPosition(Rect-Rect)中的JuliusWeetland.OptiKey.Services.WindowManipulationService.ApplyAndPersistSizeAndPosition(Rect-Rect):第523行
无效写入设置(System.String、布尔值、System.Collections.IDictionary)@System.Configuration.ClientSettingsStore
System.Configuration.ConfigurationErrorsException
调用的目标已引发异常。
mscorlib
位于System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数)
位于System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象obj,对象[]参数,对象[]参数)
位于System.Delegate.DynamicInvokeImpl(对象[]args)
位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、Int32 numArgs)
位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、Int32 numArgs、委托catchHandler)
System.Object InvokeMethod(System.Object,System.Object[],System.Signature,Boolean)@System.RuntimeMethodHandle
System.Reflection.TargetInputException异常

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

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

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