我有一个Background Worker运行程序的一部分,该程序循环遍历在运行时整理的一系列文件,并根据文件类型等执行各种SQL任务。
在处理文件时,我想一个接一个地将它们移动到另一个文件夹,以免意外处理它们。
该过程的某些部分正在锁定文件,因为循环末尾的移动命令由于锁定而失败。解除锁定(如果有)然后移动文件的最佳方法是什么?我考虑过复制然后删除,但是我怀疑删除源文件会遇到同样的问题。
计数文件子(source)
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
在此处进行填充执行以下两项操作之一-将.csv文件读入DataTable
(使用FileIO.TextFieldParser
写入数据)或将图像转换为字节使用System.IO.MemoryStream
进行编码。
在两种情况下,数据均写入SQLite表中。
答案 0 :(得分:0)
克里斯·邓纳维(Chris Dunaway)暗示,答案是在尝试移动文件之前先对close()
的ImageFieldParser和Dispose
进行操作。