Загрузка данных
hdjdjdjdjdjjd
namespace MaterialExplorer
{
public class FileItem
{
public string Name { get; set; } = string.Empty;
public string Type { get; set; } = string.Empty;
public string Size { get; set; } = string.Empty;
public string Date { get; set; } = string.Empty;
public string FullPath { get; set; } = string.Empty;
public bool IsDirectory { get; set; }
public string Icon { get; set; } = "File";
}
}
using System.Globalization;
using System.Windows.Data;
using MaterialDesignThemes.Wpf;
namespace MaterialExplorer
{
public class StringToPackIconKindConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string s && Enum.TryParse<PackIconKind>(s, true, out var kind))
return kind;
return PackIconKind.File;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
namespace MaterialExplorer
{
public partial class MainWindow : Window
{
private string currentPath;
private readonly Stack<string> backHistory = new Stack<string>();
private readonly Stack<string> forwardHistory = new Stack<string>();
public MainWindow()
{
InitializeComponent();
currentPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
LoadDirectory(currentPath);
}
private void LoadDirectory(string path, bool addToHistory = false)
{
try
{
if (!Directory.Exists(path))
{
MessageBox.Show("Папка не существует.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (addToHistory && currentPath != path)
{
backHistory.Push(currentPath);
forwardHistory.Clear();
}
currentPath = path;
PathTextBox.Text = currentPath;
var items = new List<FileItem>();
foreach (string directory in Directory.GetDirectories(currentPath))
{
try
{
DirectoryInfo info = new DirectoryInfo(directory);
items.Add(new FileItem
{
Name = info.Name,
Type = "Папка",
Size = "",
Date = info.LastWriteTime.ToString("dd.MM.yyyy HH:mm"),
FullPath = info.FullName,
IsDirectory = true,
Icon = "Folder"
});
}
catch { }
}
foreach (string file in Directory.GetFiles(currentPath))
{
try
{
FileInfo info = new FileInfo(file);
items.Add(new FileItem
{
Name = info.Name,
Type = GetFileType(info.Extension),
Size = FormatSize(info.Length),
Date = info.LastWriteTime.ToString("dd.MM.yyyy HH:mm"),
FullPath = info.FullName,
IsDirectory = false,
Icon = GetFileIcon(info.Extension)
});
}
catch { }
}
FilesListView.ItemsSource = items;
ItemsCountText.Text = $"{items.Count} элементов";
StatusText.Text = currentPath;
CurrentPathText.Text = new DirectoryInfo(currentPath).Name;
BackButton.IsEnabled = backHistory.Count > 0;
ForwardButton.IsEnabled = forwardHistory.Count > 0;
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Нет доступа к этой папке.", "Доступ запрещён", MessageBoxButton.OK, MessageBoxImage.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BackButton_Click(object sender, RoutedEventArgs e)
{
if (backHistory.Count == 0)
return;
forwardHistory.Push(currentPath);
string path = backHistory.Pop();
LoadDirectory(path);
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
if (forwardHistory.Count == 0)
return;
backHistory.Push(currentPath);
string path = forwardHistory.Pop();
LoadDirectory(path);
}
private void UpButton_Click(object sender, RoutedEventArgs e)
{
DirectoryInfo parent = Directory.GetParent(currentPath);
if (parent != null)
LoadDirectory(parent.FullName, true);
}
private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
LoadDirectory(currentPath);
}
private void PathTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
string path = PathTextBox.Text.Trim();
if (Directory.Exists(path))
LoadDirectory(path, true);
else
MessageBox.Show("Указанная папка не найдена.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void DesktopButton_Click(object sender, RoutedEventArgs e)
{
LoadDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), true);
}
private void DocumentsButton_Click(object sender, RoutedEventArgs e)
{
LoadDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), true);
}
private void DownloadsButton_Click(object sender, RoutedEventArgs e)
{
string path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Downloads");
LoadDirectory(path, true);
}
private void PicturesButton_Click(object sender, RoutedEventArgs e)
{
LoadDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), true);
}
private void MusicButton_Click(object sender, RoutedEventArgs e)
{
LoadDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), true);
}
private void NewFolderButton_Click(object sender, RoutedEventArgs e)
{
string name = "Новая папка";
string path = Path.Combine(currentPath, name);
int number = 1;
while (Directory.Exists(path))
{
path = Path.Combine(currentPath, $"{name} ({number})");
number++;
}
try
{
Directory.CreateDirectory(path);
LoadDirectory(currentPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if (FilesListView.SelectedItem is not FileItem item)
{
MessageBox.Show("Выберите файл или папку.");
return;
}
MessageBoxResult result = MessageBox.Show(
$"Удалить «{item.Name}»?",
"Подтверждение",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result != MessageBoxResult.Yes)
return;
try
{
if (item.IsDirectory)
Directory.Delete(item.FullPath, true);
else
File.Delete(item.FullPath);
LoadDirectory(currentPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка удаления");
}
}
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
string search = PathTextBox.Text;
if (!Directory.Exists(search))
return;
var items = Directory.GetFiles(search, "*", SearchOption.AllDirectories)
.Select(file =>
{
FileInfo info = new FileInfo(file);
return new FileItem
{
Name = info.Name,
Type = GetFileType(info.Extension),
Size = FormatSize(info.Length),
Date = info.LastWriteTime.ToString("dd.MM.yyyy HH:mm"),
FullPath = info.FullName,
IsDirectory = false,
Icon = GetFileIcon(info.Extension)
};
})
.ToList();
FilesListView.ItemsSource = items;
ItemsCountText.Text = $"Найдено: {items.Count}";
}
private void FilesListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FilesListView.SelectedItem is not FileItem item)
return;
if (item.IsDirectory)
{
LoadDirectory(item.FullPath, true);
}
else
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = item.FullPath,
UseShellExecute = true
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка");
}
}
}
private void FilesListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
}
private string GetFileType(string extension)
{
extension = extension.ToLower();
return extension switch
{
".txt" => "Текстовый файл",
".doc" => "Документ Word",
".docx" => "Документ Word",
".pdf" => "PDF документ",
".jpg" => "Изображение",
".jpeg" => "Изображение",
".png" => "Изображение",
".gif" => "Изображение",
".mp3" => "Аудио",
".wav" => "Аудио",
".mp4" => "Видео",
".avi" => "Видео",
".zip" => "Архив",
".rar" => "Архив",
".exe" => "Приложение",
".cs" => "C# файл",
".xaml" => "XAML файл",
_ => "Файл"
};
}
private string GetFileIcon(string extension)
{
extension = extension.ToLower();
return extension switch
{
".txt" => "FileDocument",
".doc" or ".docx" => "FileWord",
".pdf" => "FilePdfBox",
".jpg" or ".jpeg" or ".png" or ".gif" => "FileImage",
".mp3" or ".wav" => "FileMusic",
".mp4" or ".avi" => "FileVideo",
".zip" or ".rar" => "ZipBox",
".exe" => "Application",
".cs" or ".xaml" => "FileCode",
_ => "File"
};
}
private string FormatSize(long bytes)
{
if (bytes < 1024)
return $"{bytes} Б";
if (bytes < 1024 * 1024)
return $"{bytes / 1024.0:F1} KB";
if (bytes < 1024 * 1024 * 1024)
return $"{bytes / 1024.0 / 1024:F1} MB";
return $"{bytes / 1024.0 / 1024 / 1024:F1} GB";
}
}
}
djsjkdkdk