无法为第二个实例WPF保存配置文件

无法为第二个实例WPF保存配置文件,wpf,vb.net,config,Wpf,Vb.net,Config,在使用两个程序实例时,保存配置文件时遇到问题。我能够在一个简单的示例项目中重现这个问题,如下所示: Class MainWindow Dim config As System.Configuration.Configuration Public Sub New() config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.Configurati

在使用两个程序实例时,保存配置文件时遇到问题。我能够在一个简单的示例项目中重现这个问题,如下所示:

Class MainWindow
   Dim config As System.Configuration.Configuration

   Public Sub New()
      config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
   End Sub

   Protected Overrides Sub OnClosing(e As CancelEventArgs)
      config.Save(ConfigurationSaveMode.Modified, True)
   End Sub
End Class
第一个实例在关闭时保存配置,但当我尝试关闭第二个实例时,config.Save(ConfigurationSaveMode.Modified,True)抛出一个错误,表示配置文件已被另一个程序更改。我希望有人能帮我解决这个问题。提前谢谢

编辑:忘记删除MyBase调用

编辑2:尝试了一个冷冰冰的建议,但也失败了

   Protected Overrides Sub OnClosing(e As CancelEventArgs)
      Dim mdate As String = Date.Now.ToString("yyyyMMdd_HHmmss")
      Dim mptpath As String = Path.GetDirectoryName(config.FilePath) & "\" & mdate
      config.SaveAs(mdate, ConfigurationSaveMode.Full, True)
      File.Delete(fpath)
      File.Move(mptpath, fpath)
   End Sub

通过在OnClosing结束时调用OnClosing,可以创建一个循环

  Protected Overrides Sub OnClosing(e As CancelEventArgs)
      config.Save(ConfigurationSaveMode.Modified, True)
  End Sub

通过在OnClosing结束时调用OnClosing,可以创建一个循环

  Protected Overrides Sub OnClosing(e As CancelEventArgs)
      config.Save(ConfigurationSaveMode.Modified, True)
  End Sub

在那里。复制并加载配置文件,然后另存为。将saveas文件作为当前配置文件重新读取。在你出去的时候,你做的正好相反

Imports System.Configuration
Imports System.IO

Public Class Form1

    Dim config As System.Configuration.Configuration
    Dim fpath As String = ""
    Dim mptpath As String = ""
    Public Sub New()
        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
        fpath = config.FilePath
        Dim mdate As String = Date.Now.ToString("yyyyMMdd_HHmmss")
        mptpath = Path.GetDirectoryName(config.FilePath) & "\" & mdate & ".config"
        config.SaveAs(mptpath, ConfigurationSaveMode.Full, True)
        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(mptpath)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        File.Delete(fpath)
        config.SaveAs(fpath, ConfigurationSaveMode.Full, True)
        File.Delete(mptpath)
    End Sub
End Class

在那里。复制并加载配置文件,然后另存为。将saveas文件作为当前配置文件重新读取。在你出去的时候,你做的正好相反

Imports System.Configuration
Imports System.IO

Public Class Form1

    Dim config As System.Configuration.Configuration
    Dim fpath As String = ""
    Dim mptpath As String = ""
    Public Sub New()
        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
        fpath = config.FilePath
        Dim mdate As String = Date.Now.ToString("yyyyMMdd_HHmmss")
        mptpath = Path.GetDirectoryName(config.FilePath) & "\" & mdate & ".config"
        config.SaveAs(mptpath, ConfigurationSaveMode.Full, True)
        config = System.Configuration.ConfigurationManager.OpenExeConfiguration(mptpath)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        File.Delete(fpath)
        config.SaveAs(fpath, ConfigurationSaveMode.Full, True)
        File.Delete(mptpath)
    End Sub
End Class

为什么在循环中调用相同的函数?config.Save(ConfigurationSaveMode.Modified,True)MyBase.onclose(e)为什么在循环中调用相同的函数?config.Save(ConfigurationSaveMode.Modified,True)MyBase.onclose(e)转到taskmanager并关闭与应用程序相关的所有进程。它们仍然在后台运行,因为您创建了无休止的循环。您可以关闭windows上的会话并重新打开它。然后尝试代码。转到taskmanager并关闭与应用程序相关的所有进程。它们仍然在后台运行,因为您创建了无休止的循环。您可以关闭windows上的会话并重新打开它。那就试试密码。谢谢你,冷静。是的,我正在运行应用程序两次。关闭一个应用程序,试图关闭第二个应用程序,然后它崩溃。进程关闭,问题是第一个实例正在更改配置,这也是第二个实例在尝试关闭时注意到的。我需要一种方法来覆盖第一个用户所做的更改。如果你有机会使用VS,你可以很容易地重现这个问题,好吧,我错了。在测试时,我注意到SaveAs对我的实际配置文件没有影响,因为该方法创建了另一个配置文件。我试过你的建议去删除和移动,但那似乎不起作用。见Edit2Tanks,Chillzy。是的,我正在运行应用程序两次。关闭一个应用程序,试图关闭第二个应用程序,然后它崩溃。进程关闭,问题是第一个实例正在更改配置,这也是第二个实例在尝试关闭时注意到的。我需要一种方法来覆盖第一个用户所做的更改。如果你有机会使用VS,你可以很容易地重现这个问题,好吧,我错了。在测试时,我注意到SaveAs对我的实际配置文件没有影响,因为该方法创建了另一个配置文件。我试过你的建议去删除和移动,但那似乎不起作用。见第2版