正在释放文件锁定



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

随着文件的处理,我想把它们一个接一个地移到另一个文件夹,这样它们就不会被意外地重新处理。

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

计数文件子(源(

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(使用FileIO.TextFieldParser写入数据(或使用System.IO.MemoryStream将图像转换为字节码。

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

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

最新更新