我正在研究WPF。我开发了一个WPF应用程序,用户可以在其中创建和删除文件夹。文件夹的创建工作正常,但删除时访问文件夹,甚至驱动器都存在问题。按照下面提到的步骤,我可以更改所有者和权限。但它仍然无效:
代码:
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());
}
}
答案 0 :(得分:2)
我想删除一个文件夹,因此我使用Directory.Delete(folderpath);
而不是File.Delete(folderpath);
特别感谢Rahul所有人:)