Users have permission to delete files/folders that are no longer needed. This section introduces how to implement this function.
Interface layout
There is a "Delete" button in the main interface design, and its (ID) is btnDelete. After the user selects the item to be deleted in the directory browsing, click this button to complete the deletion.
The code implements
double-clicking the "Delete" button in the "Design" panel and adding an event handler for it as follows:
private void BtnDelete_Click(object sender, System.EventArgs e)
{
DeleteThings(FileList.SelectedItem.Text);
}
private void DeleteThings(string FullPath)
{
if(FullPath.IndexOf(".")>0) //Delete file{
File.Delete(FullPath);
LoadDir(CurrentPath); //Reload the current directory}
else //delete directory{
Directory.Delete(FullPath);
LoadDir(CurrentPath); //Reload the current directory}
}
When deleting, first determine whether the selected file or folder is selected. If it is a file, call the File.Delete() method; otherwise, call the Directory.Delete() method. After the deletion is successful, call the LoadDir() method to display the changed directory contents.