0

我有一个后台工作程序运行程序的一部分,该程序循环遍历在运行时整理的文件数组,根据文件类型等执行各种 SQL 任务。

一个接一个,在处理文件时,我想将它们移动到另一个文件夹,这样它们就不会被意外重新处理。

该过程的某些部分正在锁定文件,因为循环结束时的移动命令因锁定而失败。释放锁(如果有的话)然后移动文件的最佳方法是什么?我考虑先复制然后删除,但我怀疑删除源文件也会有同样的问题。

计数文件子(

Public Function count_files(ByVal location As String, ByVal Filter As String, ByVal searchOption As System.IO.SearchOption) As String()
        ' ArrayList will hold all file names
        Dim alFiles As ArrayList = New ArrayList()

    ' Create an array of filter string
    Dim MultipleFilters() As String = Filter.Split("|")

    ' for each filter find mathing file names
    For Each FileFilter As String In MultipleFilters
        ' add found file names to array list
        alFiles.AddRange(Directory.GetFiles(location, FileFilter, searchOption))
    Next

    ' returns string array of relevant file names
    Return alFiles.ToArray(Type.GetType("System.String"))
End Function

后台工作者代码。

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

'Count File Types, work out total & assign to progress bar length.
progress_total = sFiles.Count
current_count = 0

For Each FileName As String In sFiles
        current_count += 1
        currentfile = FileName
        filenamenoext = Path.GetFileNameWithoutExtension(FileName)
        extension = GetExtension(FileName)
        **DO STUFF HERE**

        'Report Progress

        BackgroundWorker1.ReportProgress(Convert.ToInt32((current_count / progress_total) * 100))

        'If we've reached here, we've done something with the file. Move it so it cannot be reprocessed without manually moving the file.
        Try
        My.Computer.FileSystem.MoveFile(currentfile, Import_Path & "processed\" & filenamenoext & extension, True)

        Catch ex As Exception
        MessageBox.Show(ex.Message, "Error Moving File", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

        System.Threading.Thread.Sleep(100)

Next

End Sub

DO STUFF HERE执行以下两件事之一 - 将 .csv 文件读入DataTable(使用 .csv 写入数据FileIO.TextFieldParser)或使用 .csv 将图像转换为字节码System.IO.MemoryStream

在这两种情况下,数据都被写入 SQLite 表。

4

1 回答 1

0

正如 Chris Dunaway 所暗示的,在尝试移动文件之前,答案是close()TextFieldParser 和相应的 Image。Dispose

于 2018-07-23T11:14:57.273 回答