How to check if a text file is open and close the text file?

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 
+1
source share
5 answers

I do not believe that there is a property that allows you to check whether the mail program is open or not.

The best practice seems to be a .close reader when this is done. (Everything in the method in which it was used.)

You can try try block handle the exception if you still get it.

I can find additional information and sample code here. Good luck.

MSDN! Streamreader

EDIT: you can check it out. IO.File

 Private Function CheckFile(ByVal filename As String) As Boolean Try System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None) FileClose(1) Return False Catch ex As Exception Return True End Try End Function 
+4
source

What about:

 If File.Exists("File1.txt") = False Then File.CreateText("File1.txt").Close() Else Exit Sub End If If File.Exists("File2.txt") = False Then File.CreateText("File2.txt").Close() Else Exit Sub End If End If 
+2
source
 Private Sub IsFileOpen(ByVal file As FileInfo) Dim stream As FileStream = Nothing Try stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None) Catch ex As IOException If IsFileLocked(ex) Then 'do something here, either wait a few seconds, close the file if you have 'a handle, make a copy of it, read it as shared (FileAccess fileAccess = FileAccess.Read, FileShare fileShare = FileShare.ReadWrite). 'I dont recommend terminating the process - which could cause corruption and lose data End If Catch ex As Exception End Try End Sub Private Shared Function IsFileLocked(exception As Exception) As Boolean Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1) Return errorCode = 32 OrElse errorCode = 33 End Function 
+1
source

The following function can be used to determine if a file is already open (True) or not (False). Then the action can be based on the results of the function.

 Public Function IsFileOpen(ByVal xFileName As String, ByVal xFileChannel As Integer) As Boolean ' ************************************************************ ' * Function: IsFileOpen ' * Purpose: To determine if a file is already open. ' * Can be used to determine if a file should be closed. ' * Syntax: ' * Dim bResult as Boolean ' * ' * bResult = IsFileOpen("C:\Test.txt", 1) ' * ' * OR ' * ' * If IsFileOpen("C:\Test.txt", 1) = True Then ' * Microsoft.VisualBasic.FileClose(1) ' * End If ' * ' ************************************************************ Try Microsoft.VisualBasic.FileOpen(xFileChannel, xFileName, OpenMode.Input, OpenAccess.Read, OpenShare.Default) Catch ' File Already Open Error Number = 55 If Trim(Err.Number.ToString) = "55" Then Return True Else Return False End If End Try End Function 
+1
source

I have a problem with the .csv file that my program attaches to email. I added code to clear the attachment collection in the MailMessage object, and then delete the MailMessage and Attachment objects after sending the mail. This seems to fix the problem.

0
source

All Articles