Vb.net 如何关闭XML文档的实例

Vb.net 如何关闭XML文档的实例,vb.net,xmldocument,Vb.net,Xmldocument,在尝试对XML文档(特别是这行代码)发出XmlDocument类的保存时: myXmlDocument.SavestrFileZillaFilePathAndName 我得到。。。在条目更新期间-保存FileZilla文件失败。进程无法访问文件“C:\Program Files x86\FileZilla Server\FileZilla Server.xml”,因为其他进程正在使用该文件 它出现在FindUpdateXmlEntries中。。。从我的UpdateFtpAccountsProce

在尝试对XML文档(特别是这行代码)发出XmlDocument类的保存时:

myXmlDocument.SavestrFileZillaFilePathAndName

我得到。。。在条目更新期间-保存FileZilla文件失败。进程无法访问文件“C:\Program Files x86\FileZilla Server\FileZilla Server.xml”,因为其他进程正在使用该文件

它出现在FindUpdateXmlEntries中。。。从我的UpdateFtpAccountsProcess中调用的方法。。。功能

我认为使用SAVE不需要关闭

它发生在函数的第二次执行时,因此文件似乎没有被关闭。它应该在第一次执行时关闭,并且不被其他进程使用

    Public Function UpdateFtpAccountsProcess(ByVal strIpAddress As String, ByVal strPassword As String, ByVal iBackupSpaceId As Integer, ByVal iFtpAccountId As Integer, ByVal strFtpAccountPassword As String, ByVal strAccountType As String) As Boolean Implements IBackupServerService.UpdateFtpAccountsProcess

        Dim strErrorMessage As String = ""

        ' Start the backup service logging.
        StartTheLog(iBackupSpaceId)

        ' Write an log entry.
        strInputLogMessage = "In UpdateFtpAccountsProcess method."
        LogAnEntry(strInputLogMessage)

        ' Validate the password arguement in order to use this WCF service method.
        ' Read the app.config files "appSettings" section to get the backup server password. If it matches, continue.
        strEncryptedBackupServerPassword = GetAppSetting(strKeyPassword)

        If strEncryptedBackupServerPassword = strPassword Then
            ' Update the FTP account's encrypted password by "backup space id" and "account type". The accounts are stored in the FileZilla Server.xml file.

            ' Read the app.config file to get the “FileZilla path and name”.
            strFileZillaFilePathAndName = GetAppSetting(strKeyFileZilla)

            ' Read the app.config file to get the “FileZilla Application path and name”.
            strFileZillaFileApplicationPathAndName = GetAppSetting(strKeyFileZillaApplication)

            If ((strFileZillaFilePathAndName = "Not Found" Or strFileZillaFilePathAndName = "Error") Or (strFileZillaFileApplicationPathAndName = "Not Found" Or strFileZillaFileApplicationPathAndName = "Error")) Then
                strErrorMessage = "During update of FTP accounts - cannot find the FileZilla entries in the app config. Space Id: " & iBackupSpaceId.ToString()
            Else
                Try
                    ' Read the XML file and update the "password" of 1 of the 2 User entries [ftp accounts] - for the "backup space id". 
                    FindAndUpdateXMLEntries(strFileZillaFilePathAndName, iBackupSpaceId, strFtpAccountPassword, strAccountType)
                    Try
                        ' Execute command to run the FileZilla application to have FileZilla re-read the configuration.
                        ReloadFileZillaConfig(strFileZillaFileApplicationPathAndName)
                    Catch ex As Exception
                        strErrorMessage = "From: ReloadFileZillaConfig() ---> " & ex.Message
                    End Try
                Catch ex As Exception
                    ' Attach the message coming from the FindAndUpdateXMLEntries() method.
                    strErrorMessage = "During update of FTP account - Space Id: " & iBackupSpaceId.ToString() & " from: FindAndUpdateXMLEntries() ---> " & ex.Message
                End Try
            End If
        Else
            strErrorMessage = "During update of FTP accounts - invalid backup server password. It must match the app.config entry. Space Id: " & iBackupSpaceId.ToString()
        End If

        If strErrorMessage <> "" Then
            Throw New System.Exception(strErrorMessage)
        End If

        ' End the backup service logging.
        EndTheLog()

        Return bSuccess = True
    End Function

    Public Sub FindAndUpdateXMLEntries(ByVal strFileZillaFilePathAndName As String, ByVal iBackupSpaceId As Integer, ByVal strFtpAccountPassword As String, ByVal strAccountType As String)

        Dim strErrorMessage As String = ""

        Dim xmlNode1 As XmlNode
        Dim xmlNode2 As XmlNode
        Dim xmlNode3 As XmlNode
        Dim strUserNodeAtrributeNameValue As String
        Dim strUnderUserNodeThisNodesAtrributeNameValue As String
        Dim bSuccess As Boolean = False

        ' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
        ' So load in the XML file.
        Dim myXmlDocument As XmlDocument = New XmlDocument()

        ' Write an log entry.
        strInputLogMessage = "In FindAndUpdateXMLEntries method."
        LogAnEntry(strInputLogMessage)

        Try
            ' Load in the XML file.
            myXmlDocument.Load(strFileZillaFilePathAndName)

            ' Use the XmlNode object that the DocumentElement property of the XmlDocument returns to manipulate an XML node.
            xmlNode1 = myXmlDocument.DocumentElement

            ' Field to match to under the "Users" node. Building the "#-Private" or "#-Private" User root node.
            strUserNodeAtrributeNameValue = iBackupSpaceId.ToString() & "-" & strAccountType.Trim()

            ' The next field to match to under the "User" node that I find.
            strUnderUserNodeThisNodesAtrributeNameValue = "Pass"

            ' Iterate thru to find the node to update.
            For Each xmlNode1 In xmlNode1.ChildNodes
                If xmlNode1.Name = "Users" Then
                    ' Find the child nodes in "Users" only.
                    For Each xmlNode2 In xmlNode1.ChildNodes
                        ' Find the "User" node that I want.
                        If xmlNode2.Attributes("Name").Value = strUserNodeAtrributeNameValue Then
                            For Each xmlNode3 In xmlNode2.ChildNodes
                                ' Find the node that I want that is under the "User" node. Looking for: <Option Name="Pass">some value</Option>
                                If xmlNode3.Attributes("Name").Value = strUnderUserNodeThisNodesAtrributeNameValue Then
                                    ' Update the XML to the new password value.
                                    xmlNode3.InnerText = strFtpAccountPassword
                                    bSuccess = True
                                    Exit For
                                End If

                                If bSuccess = True Then
                                    Exit For
                                End If
                            Next
                        End If
                    Next
                End If

                If bSuccess = True Then
                    Exit For
                End If
            Next

            If bSuccess = True Then
                Try
                    ' Use the Save method of the XmlDocument class to save the altered XML back to the input XML file.
                    myXmlDocument.Save(strFileZillaFilePathAndName)
                Catch ex As Exception
                    strErrorMessage = "During entry update - save of FileZilla file failed. " & ex.Message
                End Try
            Else
                strErrorMessage = "During entry update - No User node to update"
            End If
        Catch ex As Exception
            strErrorMessage = "During entry update - load of FileZilla file failed. " & ex.Message
        End Try

        If strErrorMessage <> "" Then
            Throw New System.Exception(strErrorMessage)
        End If
    End Sub

为什么你抓住了例外,只是为了把它扔回去?只需通知用户并继续。好的……此错误发生在函数的第二次执行时,因此文件似乎没有关闭。它应该在第一次执行时关闭,并且没有被另一个进程使用。您是否在保存它后尝试使用myXmlDocument=Nothing来强制它释放文件的句柄?四处搜索表明这不是一个罕见的问题,尽管出于某种原因,简单的重新编程尝试无法重新编程。大多数解决方案似乎是写入filestream对象,而不是使用字符串作为文件名。见Tony..我将首先尝试myXmlDocument=Nothing。