MessageBox.Show("Unable to delete file. The following exception" + " occured:\n" + ex.Message, "Failed");
}
}
Код этого метода находится в блоке try в связи с очевидным риском порождения исключения, если, например, у нас нет полномочий на удаление файла или файл был перемещен другим процессом за время, прошедшее с момента вывода данных примером, и моментом, когда пользователь нажал кнопку удаления. Мы создаем путь доступа файла, чтобы удалить его из поля CurrentParentPath, которое будет содержать путь доступа текущей папки, и формируем текст в поле textBoxFileName, которое будет содержать имя файла.
Методы для перемещения и копирования файла структурированы похожим образом:
protected void OnMoveButtonClickfobject sender, EventArgs e) {
try {
string FilePath = Path.Combine(currentFolderPath, txtBoxFileName.Text);
string Query =
"Really move the file\n" + FilePath + "\nto " + txtBoxNewPath.Text + "?";
if (MessageBox.Show(Query, "Move File?", MessageBoxButtons.YesNo) == DialogResult.Yes) {
File.Move(FilePath, txtBoxNewPath.Text);
DisplayFolderList(currentFolderPath);
}
} catch(Exception ex) {
MessageBox.Show("Unable to move file. The following exception" + " occured: \n" + ex.Message, "Failed");
}
}
protected void OnCopyButtonClick(object sender, EventArgs e) {
try {
string FilePath = Path.Combine(currentFolderPath, txt.BoxFileName.Text);
string Query = "Really copy the file\n" + FilePath + "\nto " + txtBoxNewPath.Text + "?";
if (MessageBox.Show(Query, "Copy File?", MessageBoxButtons.YesNo) == DialogResult.Yes) {
File.Copy(FilePath, txtBoxNewPath.Text);
DisplayFolderList(currentFolderPath);
}
} catch (Exception ex) {
MessageBox.Show("Unable to copy file. The following exception" + " occured:\n" + ex.Message, "Failed");
}
}