Zoya Sheikh
Zoya Sheikh

Reputation: 939

Access to path is denied when deleting folder

I am working on WPF. I developed a WPF application in which the user can create and delete folders. Creation of folder is working fine, but in deletion there are issues in accessing the folder, even the drive. By following the steps mentioned below, I was able to change the owner and the permissions. But it still doesn't work:

  1. Right-clicked folder.
  2. Selected "Security" tab.
  3. Selected "Advanced Security -> Owner" tab, then selected owner "Everyone" from existing list of owners.
  4. In the "Permissions" Tab, I assigned "Everyone" to have full control in accesing the drive.

Code:

if (DXMessageBox.Show ("Are You Sure, you Want to Delete?", "Delete Item-Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
    try
    {
        int ID = (grid.SelectedItem as Name).PK_ID;
        string folderpath = @"F:/Delete/" +(grid.SelectedItem as Name).Name1;
        string foldername = (grid.SelectedItem as Name).Name1;
        File.Delete(folderpath);

        if (!Directory.Exists(folderpath))
        {
            SqlConnection con = new SqlConnection(connection_string);
            con.Open();
            SqlCommand comm = new SqlCommand("Delete From Names where PK_ID=" + ID + ";", con);
            comm.ExecuteNonQuery();
            con.Close();
            MessageBox.Show(folderpath);
            Refresh();
        }
    }
    catch (Exception ex)
    {
        DXMessageBox.Show(ex.Message.ToString());
    }
}

Upvotes: 0

Views: 512

Answers (1)

Zoya Sheikh
Zoya Sheikh

Reputation: 939

I want to delete a folder, so i useDirectory.Delete(folderpath); instead of using File.Delete(folderpath);

Thank you everyone specially to Rahul :)

Upvotes: 2

Related Questions