I am trying to check whether file exists or not. if not create and write something into it. The file is getting created but not updating and getting error message says it is being used by another process
Code snippet:
Public shared sub MyXml()
If Not System.IO.File.Exists("configfile.xml") Then
Dim writer As New System.Xml.XmlTextWriter(fullPath.ToString + "\configfile.xml", Nothing)
End If
Dim xmlElement As New XElement("FilePath", ConfigWindow.Datapath.Text)
System.IO.File.WriteAllText(fullPath + "\configfile.xml", xmlElement.ToString())
end sub()
Any suggestions please…
WriteAllTextwill create the file, so you can simply remove theIfstatement:The reason the file is locked is that your code creates an
XmlTextWriterthat is not disposed, so it keeps holding on to the file.