Reputation: 599
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);
}
}
Upvotes: 1
Views: 870
Reputation: 599
I read this article, as suggested by @Keyur PATEL, and figuring out this is a configuration of Visual Studio, I solved it by doing the following:
Thanks for your help
Upvotes: 1
Reputation: 1486
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(System.IO.IOException)
{
// exception when file is in use or any other
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
// all other
}
}
Upvotes: 1
Reputation: 208
The user, whose account is used to run your application, must have access to that path
There are 2 ways to achieve this:
Upvotes: 0