none
How do I check for a file being open in a folder? RRS feed

  • Question

  • I have an application which analyses CSV files in a folder and exports the data they contain to a database.  The CSV files are written to the folder by a lab testing systems.  There could be a time when I attempt to open the CSV while the Lab system is writing to it.  How do I take capture the error that the file can not be opened and trigger my application to re try later on.  The application currently has a thread timer which looks at the folder.

    The collection of the file paths in the CheckFiles Sub is where I am getting the error.  It is called from within the automatic function which checks for new files in a timed process.

     Shared Sub CheckFiles()
            _allFiles = Directory.GetDirectories(PathToWatch, TypeToWatch, SearchOpt).ToList
    
            For i As Integer = Files.Count - 1 To 0 Step -1
                If Not _allFiles.Contains(Files(i)) Then
                    Files.RemoveAt(i)
                End If
            Next
            For i As Integer = _allFiles.Count - 1 To 0 Step -1
    
                If Not Files.Contains(_allFiles(i)) AndAlso CheckIfExist(_allFiles(i)) Then
    
                    NewFiles.Add(_allFiles(i))
                    Files.Add(_allFiles(i))
    
                End If
            Next
        End Sub

    Friday, November 9, 2018 4:58 PM

Answers

  • Hello,

    You can check whether csv file is open or busy with another process or not if it is not open then you can process you code

    check this link

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ca23473-0074-40a3-a070-a8e219f3190f/how-to-check-if-a-file-is-open-in-vbnet?forum=vbgeneral

    • Marked as answer by Cam Evenson Friday, November 9, 2018 7:39 PM
    Friday, November 9, 2018 5:22 PM

All replies

  • Hello,

    You can check whether csv file is open or busy with another process or not if it is not open then you can process you code

    check this link

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/5ca23473-0074-40a3-a070-a8e219f3190f/how-to-check-if-a-file-is-open-in-vbnet?forum=vbgeneral

    • Marked as answer by Cam Evenson Friday, November 9, 2018 7:39 PM
    Friday, November 9, 2018 5:22 PM
  • Hi

    Which line is causing the exception, and what is the exception message?

    What do you need the Sub to do, what info is being collected?


    Regards Les, Livingston, Scotland

    Friday, November 9, 2018 5:44 PM
  • _allFiles = Directory.GetDirectories(PathToWatch, TypeToWatch, SearchOpt).ToList 
    The line above is causing the exception.  The error I am getting is "The process cannot access the file X because it is open by another process."  What this sub does is look at the parent folder for all folders with a .D extension.  It then passes the path string into a list for further processing.  The folders with a .D are the folders which contain the CSV files I need to process. 

    Friday, November 9, 2018 6:34 PM
  • _allFiles = Directory.GetDirectories(PathToWatch, TypeToWatch, SearchOpt).ToList 
    The line above is causing the exception.  The error I am getting is "The process cannot access the file X because it is open by another process."  What this sub does is look at the parent folder for all folders with a .D extension.  It then passes the path string into a list for further processing.  The folders with a .D are the folders which contain the CSV files I need to process. 

    Hi

    What is the search pattern  TypeToWatch set to?

    EDIT:

    I have tried the following code without issue. I used it for a folder containing several sub folders, some having a .D extension and some with other extensions and some without extensions. This code found all the .D extension folders with no issues.

     Dim _allFiles As New List(Of String)
    
        _allFiles = IO.Directory.GetDirectories(PathToWatch, "*.d", IO.SearchOption.AllDirectories).ToList


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, November 9, 2018 6:54 PM
    Friday, November 9, 2018 6:40 PM
  • "The process cannot access the file X because it is open by another process."  means CSV file is in writing state in your case by another process You have to check whether it is open or not if it is open then you have to wait until another process close the file.

    Friday, November 9, 2018 6:47 PM