/
Bene_Berg
/
SchoolLibraryLite
Обзор
Документация
Войти
/
Bene_Berg
/
SchoolLibraryLite
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
SchoolLibrary/Model/ImageUploader.cs
49 строк
2 KB
Benedict
first_commit
05 июн 2025, 12:40
05 июн 2025, 12:40
a0d7a22
Код
Авторство
О чём код?
using System; using System.IO; using System.Linq; using System.Windows; using System.Collections.Generic; using SchoolLibrary.Model; namespace SchoolLibrary.Model { public class ImageUploader { public static void LoadBookCovers(string folderPath) { if (!Directory.Exists(folderPath)) { MessageBox.Show("Папка не найдена."); return; } var supportedExtensions = new[] { ".jpg", ".jpeg", ".png", ".bmp" }; var imageFiles = Directory.GetFiles(folderPath) .Where(f => supportedExtensions.Contains(Path.GetExtension(f).ToLower())) .ToList(); using (var context = new SchoolLibraryEntities()) { int updated = 0; foreach (var filePath in imageFiles) { string fileName = Path.GetFileNameWithoutExtension(filePath); if (int.TryParse(fileName, out int bookId)) { var book = context.Books.FirstOrDefault(b => b.BookID == bookId); if (book != null) { book.CoverImage = File.ReadAllBytes(filePath); updated++; } } } context.SaveChanges(); MessageBox.Show($"Загружено обложек: {updated}"); } } } }