I am trying to save a text file along this path: "C: \ Test \ test.txt", and when the file is already open, I need to check if the file is open, and I need to close it before writing it to the file.
Here is the code to save the file: Dim myfile As String = "C: \ Test \ test.txt"
'Check if file exists If System.IO.File.Exists(myfile) = True Then 'Delete it! Dim fi As New FileInfo(myfile) fi.Delete() End If Using sfdlg As New Windows.Forms.SaveFileDialog sfdlg.DefaultExt = "amk" sfdlg.Filter = "AquaMark Project|*.amk" If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then Dim SaveData As New gCanvasData IO.Directory.CreateDirectory("C:\Test") Dim w As New IO.StreamWriter("C:\Test\test.txt") Dim i As Integer For i = 0 To CheckedListBox1.Items.Count - 1 w.WriteLine(CheckedListBox1.Items.Item(i)) Next w.Close() With SaveData frmDisplay.GCanvas1.UnselectCurrentAnotate() .gAnnotates = frmDisplay.GCanvas1.gAnnotates .Image = frmDisplay.GCanvas1.Image End With Using objStreamWriter As New StreamWriter(sfdlg.FileName) Dim x As New XmlSerializer(GetType(gCanvasData)) x.Serialize(objStreamWriter, SaveData) objStreamWriter.Close() End Using End If End Using
If I do this, I can close the notepad process, but I need to close the specific open text file:
Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad") Process() = CType(Interaction.GetObject("C:\Test\test.txt"), Diagnostics.Process()) For Each p As Process In Process p.Kill() Next
user676589
source share