1

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?

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
Daryll
  • 77
  • 1
  • 2
  • 10
  • Have you tried to `.Dispose()` the web browser control so that it releases any handles it has on the file? Or, courtesy of an idea in [.NET Webbrowser Control and Dispose()](http://stackoverflow.com/questions/18055734/net-webbrowser-control-and-dispose), navigate the web browser control to `about:blank`. – Andrew Morton May 24 '14 at 21:03

1 Answers1

1

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

Daryll
  • 77
  • 1
  • 2
  • 10