20240402
This commit is contained in:
parent
d0ee17c6a5
commit
95bd8fe37f
@ -10,7 +10,7 @@ namespace anbs_cp.Classes;
|
||||
/*
|
||||
История версий
|
||||
Обновлено 2024.03.28
|
||||
* Все поля Errors, Warnings и Infos объеденены в одно Messages
|
||||
* Все поля Errors, Warnings и Infos объеденины в одно Messages
|
||||
* В ActionStateMessage добавлен параметр, определяющий тип: MessageType
|
||||
|
||||
Обновлено 2024.03.26
|
||||
@ -50,7 +50,7 @@ public class ActionState<T>: ISerializable
|
||||
/// Метод для выбора всех значений для условия
|
||||
/// </summary>
|
||||
// ReSharper disable once StaticMemberInGenericType
|
||||
public static readonly Func<ActionStateMessage, bool> SelectAll = _ => true;
|
||||
public static readonly Func<ActionStateMessage, bool> SelectAll = static _ => true;
|
||||
|
||||
#region Методы
|
||||
#region Очистка
|
||||
@ -59,7 +59,7 @@ public class ActionState<T>: ISerializable
|
||||
/// </summary>
|
||||
public void ClearErrors ()
|
||||
{
|
||||
Clear(message => message.MessageType == EActionStateMessageType.Error);
|
||||
Clear(static message => message.MessageType == EActionStateMessageType.Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -67,7 +67,7 @@ public class ActionState<T>: ISerializable
|
||||
/// </summary>
|
||||
public void ClearWarnings ()
|
||||
{
|
||||
Clear(message => message.MessageType == EActionStateMessageType.Warning);
|
||||
Clear(static message => message.MessageType == EActionStateMessageType.Warning);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -75,7 +75,7 @@ public class ActionState<T>: ISerializable
|
||||
/// </summary>
|
||||
public void ClearInfo ()
|
||||
{
|
||||
Clear(message => message.MessageType == EActionStateMessageType.Information);
|
||||
Clear(static message => message.MessageType == EActionStateMessageType.Information);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -177,17 +177,18 @@ public class ActionState<T>: ISerializable
|
||||
list.AddRange(Messages);
|
||||
break;
|
||||
case EActionStatePrintArea.ErrorsAndWarnings:
|
||||
list.AddRange(Messages.Where(message =>
|
||||
list.AddRange(Messages.Where(static message =>
|
||||
message.MessageType is EActionStateMessageType.Error or EActionStateMessageType.Warning));
|
||||
break;
|
||||
case EActionStatePrintArea.ErrorsOnly:
|
||||
list.AddRange(Messages.Where(message => message.MessageType == EActionStateMessageType.Error));
|
||||
list.AddRange(Messages.Where(static message => message.MessageType == EActionStateMessageType.Error));
|
||||
break;
|
||||
case EActionStatePrintArea.WarningsOnly:
|
||||
list.AddRange(Messages.Where(message => message.MessageType == EActionStateMessageType.Warning));
|
||||
list.AddRange(Messages.Where(static message => message.MessageType == EActionStateMessageType.Warning));
|
||||
break;
|
||||
case EActionStatePrintArea.InfosOnly:
|
||||
list.AddRange(Messages.Where(message => message.MessageType == EActionStateMessageType.Information));
|
||||
list.AddRange(Messages.Where(static message =>
|
||||
message.MessageType == EActionStateMessageType.Information));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(area), area, null);
|
||||
|
@ -1,31 +0,0 @@
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Класс -- расширение для класса File
|
||||
/// </summary>
|
||||
public static class FileExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Получает MIME-тип файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Имя файла</param>
|
||||
/// <returns>MIME-тип файла</returns>
|
||||
public static string MimeType (string filename) =>
|
||||
MimeTypes.GetMimeType(filename);
|
||||
|
||||
/// <summary>
|
||||
/// Размер файла в байтах
|
||||
/// </summary>
|
||||
/// <param name="fileName">Полное имя и путь к файлу</param>
|
||||
/// <returns>Размер файла в байтах</returns>
|
||||
public static long FileSize (string fileName) =>
|
||||
new FileInfo(fileName).Length;
|
||||
|
||||
/// <summary>
|
||||
/// Получает хэш файла
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Хэш файла в формате <see cref="FileHash"/></returns>
|
||||
public static FileHash GetHash (string fileName) =>
|
||||
new(fileName);
|
||||
}
|
113
anbs_cp/Extensions/FileExtensions.cs
Normal file
113
anbs_cp/Extensions/FileExtensions.cs
Normal file
@ -0,0 +1,113 @@
|
||||
using System.Text;
|
||||
|
||||
using anbs_cp.Classes;
|
||||
|
||||
namespace anbs_cp.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Класс -- расширение для класса File
|
||||
/// </summary>
|
||||
public static class FileExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Получает MIME-тип файла
|
||||
/// </summary>
|
||||
/// <param name="filename">Имя файла</param>
|
||||
/// <returns>MIME-тип файла</returns>
|
||||
public static string MimeType (string filename) =>
|
||||
MimeTypes.GetMimeType(filename);
|
||||
|
||||
/// <summary>
|
||||
/// Размер файла в байтах
|
||||
/// </summary>
|
||||
/// <param name="fileName">Полное имя и путь к файлу</param>
|
||||
/// <returns>Размер файла в байтах</returns>
|
||||
public static long FileSize (string fileName) =>
|
||||
new FileInfo(fileName).Length;
|
||||
|
||||
/// <summary>
|
||||
/// Получает хэш файла
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Хэш файла в формате <see cref="FileHash"/></returns>
|
||||
public static FileHash GetHash (string fileName) =>
|
||||
new(fileName);
|
||||
|
||||
/// <summary>
|
||||
/// Записывает данные <paramref name="data"/> в текстовый файл <paramref name="fileName"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Тип данных</typeparam>
|
||||
/// <param name="data">Данные</param>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <param name="useUtf8WithBom">Нужно ли использовать кодировку UTF8 With BOM или UTF8 Without BOM</param>
|
||||
public static void Write<T> (T data, string fileName, bool useUtf8WithBom = false)
|
||||
{
|
||||
// Кодировка
|
||||
Encoding filEncoding =
|
||||
useUtf8WithBom ? Encoding.UTF8 : new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
|
||||
|
||||
// Открываю файл для создания или перезаписи
|
||||
using StreamWriter writer = new(new FileStream(fileName, FileMode.OpenOrCreate), filEncoding);
|
||||
|
||||
// Записываю
|
||||
writer.WriteLine(new NewtonsoftJsonSerializer().Serialize(data));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Читает первую строку из текстового файла <paramref name="fileName"/>
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <param name="useUtf8WithBom">Нужно ли использовать кодировку UTF8 With BOM или UTF8 Without BOM</param>
|
||||
/// <typeparam name="T">Тип выходных данных</typeparam>
|
||||
/// <returns>Данные или null</returns>
|
||||
public static T? Read<T> (string fileName, bool useUtf8WithBom = false)
|
||||
{
|
||||
// Кодировка
|
||||
Encoding filEncoding =
|
||||
useUtf8WithBom ? Encoding.UTF8 : new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
|
||||
|
||||
// Открываю файл для чтения
|
||||
using StreamReader reader = new(new FileStream(fileName, FileMode.Open), filEncoding);
|
||||
|
||||
// Считываю первую запись
|
||||
string serialized = reader.ReadLine() ?? "{}";
|
||||
|
||||
// Возвращаю конвертированный тип
|
||||
return new NewtonsoftJsonSerializer().Deserialize<T>(serialized);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Читает все строки из текстового файла <paramref name="fileName"/>
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <param name="useUtf8WithBom">Нужно ли использовать кодировку UTF8 With BOM или UTF8 Without BOM</param>
|
||||
/// <typeparam name="T">Тип выходных данных</typeparam>
|
||||
/// <returns>Список данных</returns>
|
||||
public static IEnumerable<T> ReadAll<T> (string fileName, bool useUtf8WithBom = false)
|
||||
{
|
||||
// Создаю результат
|
||||
List<T> result = [];
|
||||
|
||||
// Кодировка
|
||||
Encoding filEncoding =
|
||||
useUtf8WithBom ? Encoding.UTF8 : new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
|
||||
|
||||
// Открываю файл для чтения
|
||||
using StreamReader reader = new(new FileStream(fileName, FileMode.Open), filEncoding);
|
||||
|
||||
// Пока есть строки в файле
|
||||
while (reader.ReadLine() is { } serialized)
|
||||
{
|
||||
// - десериализую их
|
||||
T? data = new NewtonsoftJsonSerializer().Deserialize<T>(serialized);
|
||||
|
||||
// - и если они не нулевые
|
||||
if (data is not null)
|
||||
// -- то добавляю их в результат
|
||||
result.Add(data);
|
||||
}
|
||||
|
||||
// Возвращаю полученный список
|
||||
return result.AsEnumerable();
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Version>2024.3.29</Version>
|
||||
<Version>2024.4.02</Version>
|
||||
<Authors>Александр Бабаев</Authors>
|
||||
<Product>Набор компонентов ANB Software</Product>
|
||||
<Description>Библиотека полезных методов языка C#</Description>
|
||||
|
@ -1,4 +1,6 @@
|
||||
using anbs_cp.Classes;
|
||||
using anbs_cp.Extensions;
|
||||
|
||||
// ReSharper disable LocalizableElement
|
||||
|
||||
namespace demo;
|
||||
@ -15,7 +17,7 @@ public sealed partial class FileHashAndMimeType: Form
|
||||
openFileDialog.ShowDialog();
|
||||
}
|
||||
|
||||
private void GetFileHashAndMimeType()
|
||||
private void GetFileHashAndMimeType ()
|
||||
{
|
||||
if (string.IsNullOrEmpty(fileNameEdt.Text))
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user