Protected void DisplayFileInfo(string fileFullName)




{

// Создание объекта класса FileInfo. В конструктор класса

// передается строка, содержащая путь к файлу

FileInfo theFile = new FileInfo(fileFullName);

if (!theFile.Exists)

throw new FileNotFoundException("Файл не найден: "

+ fileFullName);

// Вывод информации об имени файла

textBoxFileName.Text = theFile.Name;

// Вывод информации о дате создания файла

textBoxCreationTime.Text =

theFile.CreationTime.ToLongTimeString();

// Вывод информации о времени последнего доступа к файлу

textBoxLastAccessTime.Text =

theFile.LastAccessTime.ToLongDateString();

textBoxLastWriteTime.Text =

theFile.LastWriteTime.ToLongDateString();

// Вывод информации о размере файла

textBoxFileSize.Text = theFile.Length.ToString() + "bytes";

// Разрешение перемещения, удаления, копирования файлов

textBoxNewPath.Text = theFile.FullName;

textBoxNewPath.Enabled = true;

buttonCopyTo.Enabled = true;

buttonDelete.Enabled = true;

buttonMoveTo.Enabled = true; }

 

// Отображение содержимого заданной папки в двух окнах списков

// listBoxFolders и listBoxFiles

Protected void DisplayFolderList(string folderFullName)

{

// Создание объекта класса DirectoryInfo. В конструктор класса

// передается строка, содержащая путь к папке

DirectoryInfo theFolder = new DirectoryInfo(folderFullName);

// Свойство Exists позволяет проверить, существует ли объект файловой

// системы

if (!theFolder.Exists)

throw new DirectoryNotFoundException("Папка не найдена: "

+ folderFullName);

ClearAllFields();

DisableMoveFeatures();

textBoxFolder.Text = theFolder.FullName;

currentFolderPath = theFolder.FullName;

// Вывод в элемент listBoxFolders списка всех вложенных папок в папке

foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())

listBoxFolders.Items.Add(nextFolder.Name);

// Вывод в список listBoxFiles списка всех файлов в папке

Foreach (FileInfo nextFile in theFolder.GetFiles())

listBoxFiles.Items.Add(nextFile.Name);

}

 

// Щелчок по кнопке Отобразить

Protected void OnDisplayButtonClick(object sender, EventArgs e)

{

Try

{

string folderPath = textBoxInput.Text;

DirectoryInfo theFolder = new DirectoryInfo(folderPath);

If (theFolder.Exists)

{

(theFolder.FullName);

return;

}

FileInfo theFile = new FileInfo(folderPath);

If (theFile.Exists)

{

DisplayFolderList(theFile.Directory.FullName);

int index = listBoxFiles.Items.IndexOf(theFile.Name);

listBoxFiles.SetSelected(index, true);

return;

}

throw new FileNotFoundException("There is no file or folder

with" + "this name:" + textBoxInput.Text);

}

Catch (Exception ex)

{ MessageBox.Show(ex.Message);

}

}

// Объявление недоступными элементов управления для перемещения,

// удаления копирования файлов

void DisableMoveFeatures()

{

textBoxNewPath.Text = "";

textBoxNewPath.Enabled = false;

buttonCopyTo.Enabled = false;

buttonDelete.Enabled = false;

buttonMoveTo.Enabled = false;

}

// Выбор элемента в окне списка Файлы

Protected void OnListBoxFilesSelected(object sender, EventArgs e)

{

Try

{

string selectedString = listBoxFiles.SelectedItem.ToString();

string fullFileName = Path.Combine(currentFolderPath,

selectedString);

DisplayFileInfo(fullFileName);

}

Catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

 

// Выбор элемента в окне списка Папки

Protected void OnListBoxFoldersSelected(object sender, EventArgs e)

{

Try

{

string selectedString = listBoxFolders.SelectedItem.ToString();

string fullPathName = Path.Combine(currentFolderPath,

selectedString);

DisplayFolderList(fullPathName);

}

Catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

 

// Щелчок по кнопке Вверх

Protected void OnUpButtonClick(object sender, EventArgs e)

{

Try

{

string folderPath = new

FileInfo(currentFolderPath).DirectoryName;

DisplayFolderList(folderPath);

}

Catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

 

// Щелчок по кнопке Удаление

Protected void OnDeleteButtonClick(object sender, EventArgs e)

{

Try

{

string filePath = Path.Combine(currentFolderPath,

textBoxFileName.Text);

string query = "Really delete the file\n" + filePath + "?";

If (MessageBox.Show(query,

"Delete File?", MessageBoxButtons.YesNo) == Dialog

Result.Yes)

{

File.Delete(filePath);

DisplayFolderList(currentFolderPath);

}

}

Catch (Exception ex)

{

MessageBox.Show("Unable to delete file. The following exception"

+ "occurred:\n" + ex.Message, "Failed");

}

}

// Щелчок по кнопке Перемещение

protected void OnMoveButtonClick(object sender, EventArgs e)

{

try {

string filePath = Path.Combine(currentFolderPath,

textBoxFileName.Text);

string query = "Really move the file\n" + filePath + "\nto "

+ textBoxNewPath.Text + "?";



Поделиться:




Поиск по сайту

©2015-2024 poisk-ru.ru
Все права принадлежать их авторам. Данный сайт не претендует на авторства, а предоставляет бесплатное использование.
Дата создания страницы: 2022-09-06 Нарушение авторских прав и Нарушение персональных данных


Поиск по сайту: