2017-11-01 4 views
0

내 프로그램의 두 인스턴스로 작업 할 때 구성 파일을 저장하는 데 문제가 있습니다. 나는 그처럼 보이는 간단한 예제 프로젝트에서이 문제를 재현 할 수 있었다 :두 번째 인스턴스 WPF에 대해 구성 파일을 저장할 수 없습니다.

내가 두 번째 인스턴스를 닫습니다하려고으로 첫 번째 인스턴스가 있지만, 빨리, 폐쇄에 설정을 저장
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 호출을

EDIT2 삭제 잊으 : 시도 Chillzy 제안을하지만, 그것은뿐만 아니라 실패합니다.

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 
+0

루프에서 같은 함수를 호출하는 이유는 무엇입니까? config.Save (ConfigurationSaveMode.Modified, True) MyBase.OnClosing (e) –

답변

0

당신이 OnClosing

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

그것은 중요하지 않습니다. 나는 여전히 오류가 발생합니다. –

+0

귀하의 taskmanager로 이동하여 귀하의 애플 리케이션과 관련된 모든 프로세스를 닫습니다. 그 무한 루프를 만들었 기 때문에 그들은 백그라운드에서 계속 실행 중입니다. 창에서 세션을 닫고 다시 열 수 있습니다. 그런 다음 코드를 사용해보십시오. –

+0

아직 작동하지 않지만 노력에 감사드립니다. –

0

의 끝에서 OnClosing를 호출하여 루프를하고 있습니다. 구성 파일을로드 한 다음 다른 이름으로 저장합니다. 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 
+0

고마워, 칠레. 예, 응용 프로그램을 두 번 실행하고 있습니다. 하나의 앱을 닫고 두 번째 앱을 닫으려고하면 앱이 다운됩니다. –

+0

프로세스가 닫혔습니다. 문제는 첫 번째 인스턴스가 config를 변경하고 두 번째 인스턴스가 닫으려고 할 때이를 알려주는 것입니다. 첫 번째 변경 사항을 재정의 할 방법이 필요합니다. VS를 사용할 기회가 있다면이 문제를 쉽게 재현 할 수 있습니다. –

+0

Lifesaver! 고맙습니다. 분명히 Save 대신 SaveAs를 사용하는 것으로 충분합니다. –