简体   繁体   中英

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());
    }
}

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

Thank you everyone specially to Rahul :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM