重命名文件错误:该进程无法访问该文件,因为它正在被另一个进程使用 [英] Renaming File Error: The process cannot access the file because it is being used by another process

查看:78
本文介绍了重命名文件错误:该进程无法访问该文件,因为它正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,允许用户使用vb.net中的Web浏览器控件预览文件,还允许使用文本框输入其他一些详细信息.保存详细信息之后,该文件将从列表中删除,并将被添加到已完成文件的列表中,并将自动重命名.问题是,当我尝试重命名文件时,发生异常,指出该进程无法访问该文件,因为该文件正在被另一个进程使用."如何终止该过程以使我能够重命名文件?

I am creating an application that allows the user to preview files using Web Browser Control in vb.net and also allow to input some other details using textbox. After the details was saved, the file will be removed from the list and will be added to the list of finished files and will be automatically renamed. The problem is, when I am trying to rename the file, an exception occur stating "The process cannot access the file because it is being used by another process." How can I terminate the process for me to able to rename the file?

原始文件:它是完整的目的地和文件名.
SelectedFile :它是文件名.
ClearEmpData :清除字段.

Original_File: it is the complete destination and filename.
SelectedFile: it is the filename.
ClearEmpData: clear the fields.

    Private Sub btnSave_Emp_Click(sender As Object, e As EventArgs) Handles btnSave_Emp.Click
    Dim SP_Name As String = "SP_EmpData"
    Dim SP_Param As String() = {"@DType", "@Equip", "@EmpNo", "@LN", "@FN", "@Path"}
    Dim SP_Val As String() = {cbDataType_emp.Text, Equipment_Type, Emp_No, LastName, FirstName, New_Path}

    If cbDataType_emp.SelectedIndex <= 0 Then
        MyFile.Message.ShowEntryError("Please select data type.", cbDataType_emp)
        Exit Sub
    Else
        If Not MyFile.Process.MoveFiles(Root_Dir, dgvEncode.SelectedCells.Item(1).Value, cbDocType.Text) Then Exit Sub
        If Not ExecuteSaveProcedure(SP_Name, SP_Param, SP_Val) Then Exit Sub

        Original_File = dgvEncode.SelectedCells.Item(2).Value

        Dim dr As DataGridViewRow
        For Each dr In dgvEncode.SelectedRows
            dgvEncode.Rows.Remove(dr)
            dgvDone.Rows.Add(dr)
        Next
        dgvEncode.CurrentCell.Selected = dgvEncode.Rows.Count
        ClearEmpData()
        dgvDone.ClearSelection()

        Try
            My.Computer.FileSystem.RenameFile(Original_File, "xDone_" & SelectedFile)
        Catch ex As Exception
            MyFile.Message.ShowWarnning(ex.Message)
            Exit Sub
        End Try

    End If
End Sub

推荐答案

我已经解决了这个问题.我创建了一个临时文件进行预览,然后重命名了原始文件,然后删除了该临时文件.

I already solved this problem. I created a temporary file to be preview then rename the original file and then delete the temporary file.

这篇关于重命名文件错误:该进程无法访问该文件,因为它正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆