JimmyN
JimmyN

Reputation: 599

cant try catch when delete file

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

Answers (3)

JimmyN
JimmyN

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:

  • Navigate to "Debug / Exceptions / Common Language Runtime Exceptions / System"
  • Scroll down to where "NullReferenceException" is, and uncheck the "throw" checkbox, and check the "user-handled".

Thanks for your help

Upvotes: 1

Ramankingdom
Ramankingdom

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

tokenaizer
tokenaizer

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:

  1. Configure a special application pool for your application, that will run under a user that has necessary permissions (can access admin shares on remote server). So your entire application will run under that account and have all its permissions.
  2. Using impersonation to execute parts of your code under another account. This doesn't require any IIS configuration and is more secure and flexible than first way (for example? you can impersonate several accounts in a single application).

Upvotes: 0

Related Questions