如何删除另一个进程使用的文件

时间:2019-04-21 07:46:52

标签: vb.net winforms datagridview

Public Class frmMainCetegory
Private Sub frmMainCetegory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'PosDatabaseDataSet.tblItemMainCategory' table. You can move, or remove it, as needed.
    Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
    Me.CenterToParent()
    Me.btnAddNew.Select()

End Sub

Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
    Try

        Me.TblItemMainCategoryBindingSource.AddNew()
        Me.txtMainCategory.Select()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Try

        If Me.txtMainCategory.Text = "" Then
            MsgBox("Please Enter Main Category Name!", vbInformation)
            Exit Sub
        End If

        Me.imgMainCategoryImage.Image.Save(Application.StartupPath & "\Images\" & Me.txtMainCategory.Text & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)

        Me.TblItemMainCategoryBindingSource.EndEdit()
        Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
        Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
        Me.btnAddNew.Select()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Try
        Dim MainCatName As String
        MainCatName = Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value

        If Me.TblItemMainCategoryDataGridView.Rows.Count - 1 > 0 Then
            'check before delting, if this main category is used in sub category then It can not be deleted.
            Dim I As Integer
            I = frmSubCategories.TblItemSubCategoryTableAdapter.qryCountMainCategoryUnderSubCategory(MainCatName)
            If I > 0 Then
                MsgBox(MainCatName & " is used under Sub Category(s), Please Delete Sub Category(s) First!", vbOK)
                Exit Sub
            End If
        End If

        Me.TblItemMainCategoryBindingSource.RemoveCurrent()
        Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
        Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)

        'Delete the Main Category Image if this exists
        If System.IO.File.Exists(Application.StartupPath & "\Images\" & MainCatName & ".JPEG") Then
            System.IO.File.Delete(Application.StartupPath & "\Images\" & MainCatName & ".JPEG")
            MsgBox("Image Deleted Successfull")

        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
    Try

        Me.OpenFileDialog1.Filter = "Bitmaps(*.JPG, *.PNG, *.JPEG, *.BMP, *.TIF, *.GIF, *.TIFF)|"
        Me.OpenFileDialog1.FileName = ""

        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            Dim img As String = Me.OpenFileDialog1.FileName
            Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(img)
            Me.btnSave.Select()

        Else

        End If


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub TblItemMainCategoryDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles TblItemMainCategoryDataGridView.SelectionChanged
    Try

        Me.imgMainCategoryImage.Image = DirectCast(Image.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG").Clone(), Image)
        ' Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG")

    Catch ex As Exception

        Me.imgMainCategoryImage.Image = My.Resources.imgEmpty 'if image path not found then select empty image

    End Try
End Sub

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
    Me.Close()
End Sub

结束班级

上面的代码给我一个错误消息:

  

图片已在其他过程中使用,无法删除

当我删除图像文件时。图像通过DataGridView的选择事件分配给PictureBox。如上面的代码所示,由于我已经删除了DataGridView行,因此它更改了PictureBox中的图像。
在这里,我无法删除与已删除行相关的图像文件。

请指导,如果您需要其他详细信息,请告诉我。

0 个答案:

没有答案