Vb.net 释放文件锁

Vb.net 释放文件锁,vb.net,backgroundworker,movefile,Vb.net,Backgroundworker,Movefile,我有一个后台工作人员运行一个程序的一部分,该程序在运行时对一系列文件进行整理,根据文件类型等执行各种SQL任务 一个接一个地,在处理这些文件时,我想将它们移动到另一个文件夹中,这样它们就不会被意外地重新处理 由于循环结束时的move命令因锁定而失败,因此该过程的某些部分正在锁定文件。释放锁(如果有)然后移动文件的最佳方法是什么?我考虑先复制然后删除,但我怀疑它与删除源文件有相同的问题 计数文件子() 后台工作代码 Private Sub BackgroundWorker1_DoWork(send

我有一个后台工作人员运行一个程序的一部分,该程序在运行时对一系列文件进行整理,根据文件类型等执行各种SQL任务

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

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

计数文件子()

后台工作代码

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做两件事之一-将.csv文件读入
数据表(数据是使用
FileIO.TextFieldParser
写入的)或使用
System.IO.MemoryStream
将图像转换为字节码


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

正如Chris Dunaway所暗示的,答案是在尝试移动文件之前关闭相应图像的TextFieldParser和处置。

有可能“**在这里做事情**”中发生的任何事情都是锁定文件的原因。这是猜测,因为您没有提供该代码的任何详细信息。在上面的帖子中有更多数据…假设您已经隐藏了错误,这是相当安全的。值得注意的是,MoveFile()实际上并不关心锁,它所做的只是移动目录项,而这是无法锁定的。除非您将其移动到另一个磁盘驱动器,否则它必须求助于完整副本,并且锁确实会起作用。这不是您通常喜欢的。您确定在读取.csv文件或打开MemoryStream文件时,您正确地关闭了所有文件吗?那是我的猜测。克里斯,这就是问题所在。我通过在循环末尾添加一个
parser.close()
修复了TextParser,但我需要查看使用
MemoryStream
的函数,以查看关闭/处置它的最佳位置。我想我可能需要将其更新为使用
使用
,请随意做一个正确的答案,我会给你最好的答案。
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