I want to delete a file, in case it is locked by another process even though I have set try catch, but the program is still dark cash at fi.Delete(), so how to fix it.
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'H:\J\R\sound.MP4' is denied.
private void GView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
string cellValue = GView.Rows[e.RowIndex].Cells["NAME"].Value.ToString();
var confirmResult = MessageBox.Show("delete this item " + cellValue,
"Confirm Delete!!",
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.Yes)
{
System.IO.FileInfo fi = new System.IO.FileInfo(cellValue);
fi.Delete();
}
else
{
}
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message);
}
}