The following base classes in the MaksIT.Core.Abstractions
namespace provide a foundation for implementing domain, DTO, and Web API models, ensuring consistency and maintainability in application design.
Represents the base class for all domain objects in the application.
- Serves as the foundation for all domain objects.
- Provides a place to include shared logic or properties for domain-level entities in the future.
Represents a base class for domain documents with a unique identifier.
- Extends
DomainObjectBase
to include an identifier. - Provides a common structure for domain entities that need unique IDs.
public class UserDomainDocument : DomainDocumentBase<Guid> {
public UserDomainDocument(Guid id) : base(id) {
}
}
Represents the base class for all Data Transfer Objects (DTOs).
- Serves as the foundation for all DTOs.
- Provides a place to include shared logic or properties for DTOs in the future.
Represents a base class for DTOs with a unique identifier.
- Extends
DtoObjectBase
to include an identifier. - Provides a common structure for DTOs that need unique IDs.
public class UserDto : DtoDocumentBase<Guid> {
public required string Name { get; set; }
}
Represents the base class for Web API request models.
- Serves as a foundation for request models used in Web API endpoints.
- Provides a common structure for request validation or shared properties.
public class CreateUserRequest : RequestModelBase {
public required string Name { get; set; }
}
Represents the base class for Web API response models.
- Serves as a foundation for response models returned by Web API endpoints.
- Provides a common structure for standardizing API responses.
public class UserResponse : ResponseModelBase {
public required Guid Id { get; set; }
public required string Name { get; set; }
}
-
Consistency:
- Ensures a uniform structure for domain, DTO, and Web API models.
-
Extensibility:
- Base classes can be extended to include shared properties or methods as needed.
-
Type Safety:
- Generic identifiers (
T
) ensure type safety for domain documents and DTOs.
- Generic identifiers (
-
Reusability:
- Common logic or properties can be added to base classes and reused across the application.
// Domain Class
public class ProductDomain : DomainDocumentBase<int> {
public ProductDomain(int id) : base(id) { }
public string Name { get; set; } = string.Empty;
}
// DTO Class
public class ProductDto : DtoDocumentBase<int> {
public required string Name { get; set; }
}
// Web API Request Model
public class CreateProductRequest : RequestModelBase {
public required string Name { get; set; }
}
// Web API Response Model
public class ProductResponse : ResponseModelBase {
public required int Id { get; set; }
public required string Name { get; set; }
}
-
Keep Base Classes Lightweight:
- Avoid adding unnecessary properties or methods to base classes.
-
Encapsulation:
- Use base classes to enforce encapsulation and shared behavior across entities.
-
Validation:
- Extend
RequestModelBase
orResponseModelBase
to include validation logic if needed.
- Extend
This structure promotes clean code principles, reducing redundancy and improving maintainability across the application layers.
The Enumeration
class in the MaksIT.Core.Abstractions
namespace provides a base class for creating strongly-typed enumerations. It enables you to define enumerable constants with additional functionality, such as methods for querying, comparing, and parsing enumerations.
-
Strongly-Typed Enumerations:
- Combines the clarity of enums with the extensibility of classes.
- Supports additional fields, methods, or logic as needed.
-
Reflection Support:
- Dynamically retrieve all enumeration values with
GetAll
.
- Dynamically retrieve all enumeration values with
-
Parsing Capabilities:
- Retrieve enumeration values by ID or display name.
-
Comparison and Equality:
- Fully implements equality and comparison operators for use in collections and sorting.
public class MyEnumeration : Enumeration {
public static readonly MyEnumeration Value1 = new(1, "Value One");
public static readonly MyEnumeration Value2 = new(2, "Value Two");
private MyEnumeration(int id, string name) : base(id, name) { }
}
var allValues = Enumeration.GetAll<MyEnumeration>();
allValues.ToList().ForEach(Console.WriteLine);
var valueById = Enumeration.FromValue<MyEnumeration>(1);
var valueByName = Enumeration.FromDisplayName<MyEnumeration>("Value One");
Console.WriteLine(valueById); // Output: Value One
Console.WriteLine(valueByName); // Output: Value One
var difference = Enumeration.AbsoluteDifference(MyEnumeration.Value1, MyEnumeration.Value2);
Console.WriteLine($"Absolute Difference: {difference}"); // Output: 1
var values = new List<MyEnumeration> { MyEnumeration.Value2, MyEnumeration.Value1 };
values.Sort(); // Orders by ID
-
Extend for Specific Enums:
- Create specific subclasses for each enumeration type.
-
Avoid Duplicates:
- Ensure unique IDs and names for each enumeration value.
-
Use Reflection Sparingly:
- Avoid calling
GetAll
in performance-critical paths.
- Avoid calling
The Enumeration
class provides a powerful alternative to traditional enums, offering flexibility and functionality for scenarios requiring additional metadata or logic.
The DataTableExtensions
class is a static utility class in the MaksIT.Core.Extensions
namespace. It provides extension methods for working with DataTable
objects, enabling functionality such as counting duplicate rows between two DataTable
instances and retrieving distinct records based on specified columns.
Compares two DataTable
instances (dt1
and dt2
) row by row and counts the number of duplicate rows.
DataTable table1 = new DataTable();
DataTable table2 = new DataTable();
// Populate table1 and table2...
int duplicateCount = table1.DuplicatesCount(table2);
Console.WriteLine($"Number of duplicate rows: {duplicateCount}");
Filters a DataTable
to return distinct rows based on specified columns.
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Age");
// Populate the table...
string[] columns = { "Name", "Age" };
DataTable distinctTable = table.DistinctRecords(columns);
Console.WriteLine("Distinct rows based on Name and Age:");
foreach (DataRow row in distinctTable.Rows)
{
Console.WriteLine($"{row["Name"]}, {row["Age"]}");
}
-
Performance Considerations:
- The
DuplicatesCount
method uses nested loops, which may impact performance on large data sets. Consider optimization for large tables. - The
DistinctRecords
method leveragesDataView.ToTable
, which is efficient for retrieving distinct records but requires valid column names.
- The
-
Edge Cases:
- Ensure both
DataTable
instances inDuplicatesCount
have compatible schemas for meaningful results. - For
DistinctRecords
, columns that do not exist in the sourceDataTable
will throw an exception.
- Ensure both
-
Null Handling:
- Both methods ensure that
null
inputs result in a clear exception message.
- Both methods ensure that
This class adds flexibility when working with DataTable
objects, making it easier to handle duplicate detection and record filtration tasks programmatically.
The DateTimeExtensions
class in the MaksIT.Core.Extensions
namespace provides a set of extension methods for the DateTime
type, enabling enhanced date manipulation capabilities. These methods simplify common date-related tasks, such as adding workdays, finding specific weekdays, or determining month/year boundaries.
Adds a specified number of workdays (excluding weekends and holidays) to the given date.
DateTime today = DateTime.Today;
IHolidayCalendar holidayCalendar = new CustomHolidayCalendar();
DateTime futureWorkday = today.AddWorkdays(5, holidayCalendar);
Finds the next occurrence of a specified weekday starting from a given date.
DateTime today = DateTime.Today;
DateTime nextMonday = today.NextWeekday(DayOfWeek.Monday);
Calculates the time span until the next occurrence of a specified weekday.
DateTime today = DateTime.Today;
TimeSpan timeToNextFriday = today.ToNextWeekday(DayOfWeek.Friday);
Gets the date of the last day of the next month, preserving the time of the given date.
DateTime today = DateTime.Today;
DateTime nextMonthEnd = today.NextEndOfMonth();
Gets the date of the last day of the current month, preserving the time of the given date.
Gets the first day of the current month, preserving the time of the given date.
Gets the first or last day of the current year, preserving the time of the given date.
Checks whether the given date is at the end of the month, the beginning of the month, or the end of the year.
Checks whether two dates belong to the same month and year.
Calculates the number of complete calendar years between two dates.
DateTime birthDate = new DateTime(1990, 5, 15);
DateTime today = DateTime.Today;
int age = birthDate.GetDifferenceInYears(today);
The IHolidayCalendar
interface defines a method to determine if a specific date is a holiday.
public class CustomHolidayCalendar : IHolidayCalendar {
private readonly HashSet<DateTime> holidays = new() { new DateTime(2024, 1, 1) };
public bool Contains(DateTime date) => holidays.Contains(date.Date);
}
This class, with its utility methods, simplifies date calculations for workday management, boundaries, and comparisons. It is especially useful for business and scheduling applications.
The ExpressionExtensions
class in the MaksIT.Core.Extensions
namespace provides extension methods for working with LINQ expressions. These methods simplify tasks like combining multiple expressions into a single logical predicate, which is especially useful in building dynamic queries.
- The method reuses the parameter from the
first
expression. - It replaces the parameter in the
second
expression with the same parameter from thefirst
expression using theSubstituteParameterVisitor
class. - Combines the bodies of
first
andsecond
usingExpression.AndAlso
.
Expression<Func<int, bool>> isEven = x => x % 2 == 0;
Expression<Func<int, bool>> isPositive = x => x > 0;
var combinedExpression = isEven.CombineWith(isPositive);
var numbers = new[] { -2, -1, 0, 1, 2, 3, 4 };
var filteredNumbers = numbers.AsQueryable().Where(combinedExpression).ToList();
Console.WriteLine(string.Join(", ", filteredNumbers)); // Output: 2, 4
-
Use Cases:
- Dynamically building LINQ queries with multiple predicates.
- Simplifying expression tree manipulation in repository patterns or query builders.
-
Performance:
- The method uses expression visitors, which is efficient for modifying expression trees without evaluating them.
-
Limitations:
- Works only with
Expression<Func<T, bool>>
predicates. - Both
first
andsecond
must target the same typeT
.
- Works only with
This utility makes it easy to compose complex logical expressions dynamically, a common need in advanced querying scenarios like filtering or search functionalities.
The FormatsExtensions
class in the MaksIT.Core.Extensions
namespace provides utility methods for working with TAR file creation from directories. These methods simplify the process of compressing directories into TAR files while handling various edge cases, such as invalid paths or empty directories.
Attempts to create a TAR file from the contents of a specified directory. Returns true
if the operation succeeds, or false
if it fails due to invalid input or other errors.
string sourceDirectory
: The path to the source directory to be compressed.string outputTarPath
: The path where the TAR file will be created.
string sourceDirectory = @"C:\MyFolder";
string outputTarPath = @"C:\MyFolder.tar";
if (FormatsExtensions.TryCreateTarFromDirectory(sourceDirectory, outputTarPath)) {
Console.WriteLine("TAR file created successfully.");
}
else {
Console.WriteLine("Failed to create TAR file.");
}
-
Error Handling:
- Returns
false
for invalid source directories, empty directories, or inaccessible output paths.
- Returns
-
Cross-Platform Compatibility:
- Designed to work on platforms supported by .NET 8.
-
Ease of Use:
- Simplifies the process of creating TAR files with minimal code.
-
Use Cases:
- Archiving directories for backup or distribution.
- Automating file compression tasks in applications.
-
Edge Cases:
- If the source directory does not exist, is empty, or the output path is invalid, the method will return
false
. - If the output file is locked or cannot be created, the method will also return
false
.
- If the source directory does not exist, is empty, or the output path is invalid, the method will return
// Valid input
string sourceDirectory = @"C:\MyFolder";
string outputTarPath = @"C:\MyFolder.tar";
if (FormatsExtensions.TryCreateTarFromDirectory(sourceDirectory, outputTarPath)) {
Console.WriteLine("TAR file created successfully.");
}
else {
Console.WriteLine("Failed to create TAR file.");
}
// Invalid source directory
string invalidSourceDirectory = @"C:\NonExistentFolder";
if (!FormatsExtensions.TryCreateTarFromDirectory(invalidSourceDirectory, outputTarPath)) {
Console.WriteLine("Source directory does not exist.");
}
The GuidExtensions
class in the MaksIT.Core.Extensions
namespace provides an extension method for converting a Guid
to a nullable Guid?
. This is useful in scenarios where the default value of Guid.Empty
needs to be treated as null
.
Converts a Guid
to a nullable Guid?
. If the Guid
is Guid.Empty
, the method returns null
; otherwise, it returns the Guid
.
Guid id1 = Guid.NewGuid();
Guid id2 = Guid.Empty;
Guid? nullableId1 = id1.ToNullable(); // Returns the value of id1
Guid? nullableId2 = id2.ToNullable(); // Returns null
Console.WriteLine(nullableId1); // Outputs the GUID value
Console.WriteLine(nullableId2); // Outputs nothing (null)
-
Use Cases:
- Simplifies handling of
Guid
values in APIs, databases, or validation logic whereGuid.Empty
is treated asnull
. - Useful in scenarios where nullable values improve data handling or reduce ambiguity in business logic.
- Simplifies handling of
-
Edge Cases:
- If the input
Guid
isGuid.Empty
, this method always returnsnull
, regardless of context. - Any valid
Guid
other thanGuid.Empty
is returned unchanged.
- If the input
-
Performance:
- This method performs a simple comparison and is highly efficient.
This extension adds clarity and reduces boilerplate code when working with Guid
values, particularly in data validation or transformation workflows.
The ObjectExtensions
class in the MaksIT.Core.Extensions
namespace provides extension methods for serializing objects to JSON strings using System.Text.Json
. These methods allow flexible JSON serialization with optional custom converters.
Converts an object of type T
to a JSON string using default serialization options.
var person = new { Name = "John", Age = 30 };
string json = person.ToJson();
Console.WriteLine(json); // Output: {"name":"John","age":30}
object? nullObject = null;
string nullJson = nullObject.ToJson();
Console.WriteLine(nullJson); // Output: {}
Converts an object of type T
to a JSON string using default serialization options and additional custom converters.
var person = new { Name = "Alice", BirthDate = DateTime.UtcNow };
var converters = new List<JsonConverter> {
new JsonStringEnumConverter(), // Example custom converter
};
string jsonWithConverters = person.ToJson(converters);
Console.WriteLine(jsonWithConverters);
// Output with converters may vary depending on implementation.
-
Use Cases:
- Simplifies JSON serialization for objects with optional custom converters.
- Handles
null
objects gracefully by returning"{}"
.
-
Edge Cases:
- When
obj
isnull
, the method always returns"{}"
, even if custom converters are provided. - If
converters
isnull
or empty, serialization uses only the default options.
- When
-
Performance:
- Serialization leverages
System.Text.Json
, which is optimized for .NET applications.
- Serialization leverages
These extension methods simplify object-to-JSON serialization while offering flexibility with custom serialization options, making them ideal for use in APIs and other data exchange scenarios.
The StringExtensions
class in the MaksIT.Core.Extensions
namespace is an extensive library of utility methods for string manipulation, type conversions, and various transformations. It includes functionality for handling substrings, parsing, validations, and formatting, among other capabilities.
Checks if a string matches a pattern with SQL-like wildcard syntax (*
and ?
).
Usage:
bool result = "example".Like("exa*e");
Extract portions of a string based on character count or position.
Usage:
string result = "example".Left(3); // "exa"
Convert strings to numeric types or nullable equivalents.
Usage:
int value = "123".ToInteger(); // 123
int? nullableValue = "".ToNullableInt(); // null
Parse strings into DateTime
or DateTime?
with optional formats.
Usage:
DateTime date = "01/01/2023".ToDate();
DateTime? nullableDate = "".ToNullableDateTime();
Converts strings like "true"
, "yes"
, or "1"
to bool
.
Usage:
bool result = "yes".ToBool(); // true
bool? nullableResult = "".ToNullableBool(); // null
Parse strings to enums, with support for [Display(Name = "...")]
attributes.
Usage:
DayOfWeek day = "Monday".ToEnum<DayOfWeek>();
DayOfWeek? nullableDay = "".ToNullableEnum<DayOfWeek>();
Transforms the casing of strings.
Usage:
string result = "hello world".ToTitle(); // "Hello world"
Reads a CSV file and converts it into a DataTable
.
Usage:
DataTable table = "path/to/file.csv".CSVToDataTable();
Checks whether a string matches specific formats or conditions.
Usage:
bool isEmail = "test@example.com".IsValidEmail(); // true
Converts HTML content to plain text by removing tags and entities.
Usage:
string plainText = "<p>Hello</p>".HtmlToPlainText(); // "Hello"
Extracts distinct URLs from a string.
Usage:
var urls = "Visit http://example.com".ExtractUrls();
Formats a string with specified arguments.
public static string Format(this string s, params object[] args);
Usage:
string formatted = "Hello {0}".Format("World"); // "Hello World"
Deserializes a JSON string into an object of type T
.
Usage:
var obj = "{\"Name\":\"John\"}".ToObject<MyClass>();
Generates a Guid
from a string using MD5 hashing.
Usage:
Guid guid = "example".ToGuid();
Splits a string by a character and trims each segment.
Usage:
string[] parts = "a, b, c".StringSplit(',');
- Performance: Methods are optimized for common scenarios.
- Error Handling: Includes graceful null handling in many cases.
- Versatility: Suitable for APIs, data processing, and utility libraries.
These methods enhance string handling capabilities and simplify common operations.
The file logging system in the MaksIT.Core.Logging
namespace provides a custom implementation of the ILogger
interface. It includes a FileLogger
to log messages to a file, a FileLoggerProvider
to create logger instances, and an extension method to integrate the file logger into the Microsoft.Extensions.Logging
framework.
var services = new ServiceCollection();
services.AddLogging(builder => builder.AddFile("logs.txt"));
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<FileLogger>>();
logger.LogInformation("Logging to file!");
- Thread Safety: Ensures thread-safe writes using a lock mechanism.
- Customizable File Path: Specify the log file location during configuration.
- Integration: Easily integrates with
Microsoft.Extensions.Logging
. - Log Levels: Supports all standard log levels.
- Performance: The file operations use
File.AppendAllText
, which may have overhead for high-frequency logging. For high-throughput scenarios, consider using a buffered approach. - File Management: Ensure proper file rotation and cleanup to avoid large log files.
- Scoping: Scoping is not supported in this implementation.
This logging system is ideal for lightweight, file-based logging needs and integrates seamlessly into existing .NET applications using the Microsoft.Extensions.Logging
framework.
The NetworkConnection
class in the MaksIT.Core.Networking.Windows
namespace provides a Windows-only implementation for managing connections to network shares. It uses the Windows mpr.dll
to create and manage connections.
MaksIT.Core.Networking.Windows
public class NetworkConnection : IDisposable
This class is specifically designed for Windows platforms and allows connecting to and disconnecting from network resources such as shared drives or directories.
ILogger<NetworkConnection> logger = new LoggerFactory().CreateLogger<NetworkConnection>();
NetworkCredential credentials = new NetworkCredential("username", "password");
if (NetworkConnection.TryCreate(logger, @"\\server\share", credentials, out var connection, out var errorMessage)) {
Console.WriteLine("Connected successfully.");
connection.Dispose(); // Always dispose after use.
} else {
Console.WriteLine($"Failed to connect: {errorMessage}");
}
-
Windows-Only:
- This class is supported only on Windows platforms. A check ensures it is not used on other operating systems.
-
Thread-Safe:
- Proper thread-safety measures are in place for connection and disconnection.
-
Error Handling:
- Detailed error messages are provided for connection failures.
-
Performance:
- The implementation directly interacts with the Windows API, making it lightweight but platform-dependent.
-
Usage:
- Always dispose of the connection using
Dispose
to ensure proper resource cleanup.
- Always dispose of the connection using
-
Limitations:
- The class does not support non-Windows platforms and will throw a
PlatformNotSupportedException
if used elsewhere.
- The class does not support non-Windows platforms and will throw a
This class is ideal for managing secure connections to shared network resources in Windows environments.
The PingPort
class in the MaksIT.Core.Networking
namespace provides utility methods to check the reachability of a host on specified TCP or UDP ports. These methods help in network diagnostics by attempting to establish a connection to the target host and port.
Tries to establish a connection to a host on a specified TCP port.
if (PingPort.TryHostPort("example.com", 80, out var errorMessage)) {
Console.WriteLine("TCP port is reachable.");
} else {
Console.WriteLine($"Failed to reach TCP port: {errorMessage}");
}
Tries to send and receive a message to/from a host on a specified UDP port.
if (PingPort.TryUDPPort("example.com", 123, out var errorMessage)) {
Console.WriteLine("UDP port is reachable.");
} else {
Console.WriteLine($"Failed to reach UDP port: {errorMessage}");
}
-
Protocol Support:
TryHostPort
: For TCP ports.TryUDPPort
: For UDP ports.
-
Timeout Handling:
TryHostPort
: Waits for up to 5 seconds for a connection.TryUDPPort
: Sets a receive timeout of 5 seconds.
-
Error Reporting:
- Returns a clear error message if the operation fails.
-
Lightweight:
- Both methods are simple and do not require additional dependencies.
-
Performance:
- Designed for quick diagnostics and has a default timeout of 5 seconds to avoid blocking indefinitely.
-
Error Handling:
- Gracefully handles exceptions and provides detailed error messages for debugging.
-
Limitations:
- The reachability of a UDP port depends on whether the target host sends a response to the initial message.
- Cannot guarantee service availability, only the ability to establish a connection.
This class is ideal for quick and lightweight network port checks, particularly in diagnostic or troubleshooting tools.
The AESGCMUtility
class in the MaksIT.Core.Security
namespace provides utility methods for encryption and decryption using AES-GCM (Advanced Encryption Standard in Galois/Counter Mode). It includes functionality to securely encrypt and decrypt data and generate encryption keys.
Encrypts data using AES-GCM with a base64-encoded key. The encrypted data includes the ciphertext, authentication tag, and IV.
var key = AESGCMUtility.GenerateKeyBase64();
var data = Encoding.UTF8.GetBytes("Sensitive data");
if (AESGCMUtility.TryEncryptData(data, key, out var encryptedData, out var error)) {
Console.WriteLine("Encryption successful.");
} else {
Console.WriteLine($"Encryption failed: {error}");
}
Decrypts data encrypted with AES-GCM using a base64-encoded key. The data must include the ciphertext, authentication tag, and IV.
if (AESGCMUtility.TryDecryptData(encryptedData, key, out var decryptedData, out var error)) {
Console.WriteLine($"Decryption successful: {Encoding.UTF8.GetString(decryptedData)}");
} else {
Console.WriteLine($"Decryption failed: {error}");
}
Generates a secure, random 256-bit AES key and returns it as a base64-encoded string.
var key = AESGCMUtility.GenerateKeyBase64();
Console.WriteLine($"Generated Key: {key}");
-
Secure Encryption:
- Uses AES-GCM, a secure encryption mode with built-in authentication.
-
Data Integrity:
- Includes an authentication tag to ensure the ciphertext has not been tampered with.
-
Key Generation:
- Supports generating random, secure AES keys.
-
Error Handling:
- Provides descriptive error messages for failed encryption or decryption.
-
Flexible Output:
- Encrypted data includes the IV, tag, and ciphertext, ensuring portability and compatibility.
-
Security:
- Always store keys securely, e.g., in a hardware security module (HSM) or a secure configuration service.
- Avoid reusing IVs with the same key, as this compromises security.
-
Performance:
- AES-GCM is optimized for modern hardware and offers efficient encryption and decryption.
-
Error Handling:
- The methods return
false
and provide detailed error messages if any issue occurs.
- The methods return
// Generate a key
var key = AESGCMUtility.GenerateKeyBase64();
// Data to encrypt
var plaintext = Encoding.UTF8.GetBytes("Confidential data");
// Encrypt
if (AESGCMUtility.TryEncryptData(plaintext, key, out var encryptedData, out var encryptError)) {
Console.WriteLine("Encryption successful.");
// Decrypt
if (AESGCMUtility.TryDecryptData(encryptedData, key, out var decryptedData, out var decryptError)) {
Console.WriteLine($"Decryption successful: {Encoding.UTF8.GetString(decryptedData)}");
} else {
Console.WriteLine($"Decryption failed: {decryptError}");
}
} else {
Console.WriteLine($"Encryption failed: {encryptError}");
}
This utility simplifies secure encryption and decryption with AES-GCM and is suitable for sensitive data in .NET applications.
The Base32Encoder
class in the MaksIT.Core.Security
namespace provides methods to encode binary data into Base32 and decode Base32 strings back into binary data. Base32 encoding is commonly used in scenarios where binary data needs to be represented in a textual format.
Encodes binary data into a Base32 string.
- Converts binary data into Base32 using 5-bit blocks.
- Adds padding (
=
) to ensure the encoded string length is a multiple of 8.
byte[] data = Encoding.UTF8.GetBytes("Hello World");
if (Base32Encoder.TryEncode(data, out var encoded, out var errorMessage)) {
Console.WriteLine($"Encoded: {encoded}");
} else {
Console.WriteLine($"Encoding failed: {errorMessage}");
}
Decodes a Base32 string back into binary data.
- Removes any padding characters (
=
) from the input string. - Converts the Base32 string back into binary data using 5-bit blocks.
string base32 = "JBSWY3DPEBLW64TMMQ======";
if (Base32Encoder.TryDecode(base32, out var decoded, out var errorMessage)) {
Console.WriteLine($"Decoded: {Encoding.UTF8.GetString(decoded)}");
} else {
Console.WriteLine($"Decoding failed: {errorMessage}");
}
-
Encoding and Decoding:
- Converts binary data into a Base32 string and vice versa.
-
Padding:
- Ensures Base32-encoded strings are properly padded to be a multiple of 8 characters.
-
Error Handling:
- Provides descriptive error messages if encoding or decoding fails.
-
Compatibility:
- Adheres to standard Base32 encoding, making it compatible with other Base32 implementations.
-
Use Cases:
- Suitable for encoding binary data such as keys, tokens, and identifiers in a human-readable format.
- Commonly used in applications like TOTP (Time-Based One-Time Passwords).
-
Limitations:
- Input strings for decoding must strictly follow the Base32 format.
- Encoding may include padding characters (
=
), which are not always required in certain applications.
-
Performance:
- Efficient for small to medium-sized data. For large data, consider stream-based approaches.
// Original data
byte[] originalData = Encoding.UTF8.GetBytes("Example Data");
// Encode to Base32
if (Base32Encoder.TryEncode(originalData, out var encoded, out var encodeError)) {
Console.WriteLine($"Encoded: {encoded}");
// Decode back to binary
if (Base32Encoder.TryDecode(encoded, out var decodedData, out var decodeError)) {
Console.WriteLine($"Decoded: {Encoding.UTF8.GetString(decodedData)}");
} else {
Console.WriteLine($"Decoding failed: {decodeError}");
}
} else {
Console.WriteLine($"Encoding failed: {encodeError}");
}
This utility provides a robust implementation of Base32 encoding and decoding, making it ideal for use in security and data encoding scenarios.
The ChecksumUtility
class in the MaksIT.Core.Security
namespace provides methods to calculate and verify CRC32 checksums for byte arrays and files. This utility is designed for efficient data integrity checks and supports chunk-based processing for large files.
Calculates the CRC32 checksum for a given byte array.
byte[] data = Encoding.UTF8.GetBytes("Sample data");
if (ChecksumUtility.TryCalculateCRC32Checksum(data, out var checksum, out var errorMessage)) {
Console.WriteLine($"CRC32 Checksum: {checksum}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Calculates the CRC32 checksum for a file.
if (ChecksumUtility.TryCalculateCRC32ChecksumFromFile("sample.txt", out var checksum, out var errorMessage)) {
Console.WriteLine($"File CRC32 Checksum: {checksum}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Calculates the CRC32 checksum for a file in chunks, optimizing for large files.
if (ChecksumUtility.TryCalculateCRC32ChecksumFromFileInChunks("largefile.txt", out var checksum, out var errorMessage)) {
Console.WriteLine($"Chunked File CRC32 Checksum: {checksum}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Verifies that a byte array matches a given CRC32 checksum.
if (ChecksumUtility.VerifyCRC32Checksum(data, "5d41402abc4b2a76b9719d911017c592")) {
Console.WriteLine("Checksum matches.");
} else {
Console.WriteLine("Checksum does not match.");
}
Verifies that a file matches a given CRC32 checksum.
if (ChecksumUtility.VerifyCRC32ChecksumFromFile("sample.txt", "5d41402abc4b2a76b9719d911017c592")) {
Console.WriteLine("File checksum matches.");
} else {
Console.WriteLine("File checksum does not match.");
}
Verifies that a file matches a given CRC32 checksum using chunk-based processing.
if (ChecksumUtility.VerifyCRC32ChecksumFromFileInChunks("largefile.txt", "5d41402abc4b2a76b9719d911017c592")) {
Console.WriteLine("Chunked file checksum matches.");
} else {
Console.WriteLine("Chunked file checksum does not match.");
}
-
CRC32 Checksum Calculation:
- Supports byte arrays and files.
- Provides chunk-based processing for large files to optimize memory usage.
-
Verification:
- Verifies whether data or files match a given CRC32 checksum.
-
Error Handling:
- Outputs descriptive error messages for all failures.
-
Flexible File Handling:
- Offers both full-file and chunked approaches for checksum calculations.
-
Use Cases:
- Validate data integrity during file transfers or storage.
- Verify file authenticity by comparing checksums.
-
Performance:
- Chunked methods are recommended for large files to reduce memory consumption.
-
Limitations:
- CRC32 is not suitable for cryptographic purposes due to its vulnerability to collisions.
string filePath = "testfile.txt";
string expectedChecksum = "5d41402abc4b2a76b9719d911017c592";
// Calculate checksum
if (ChecksumUtility.TryCalculateCRC32ChecksumFromFile(filePath, out var checksum, out var error)) {
Console.WriteLine($"Calculated Checksum: {checksum}");
// Verify checksum
if (ChecksumUtility.VerifyCRC32ChecksumFromFile(filePath, expectedChecksum)) {
Console.WriteLine("Checksum verification successful.");
} else {
Console.WriteLine("Checksum verification failed.");
}
} else {
Console.WriteLine($"Error calculating checksum: {error}");
}
This utility provides efficient and reliable CRC32 checksum calculation and verification for various data integrity scenarios.
The Crc32
class in the MaksIT.Core.Security
namespace provides an implementation of the CRC32 (Cyclic Redundancy Check) hash algorithm. This class extends the HashAlgorithm
base class, enabling usage in cryptographic workflows while also providing utility methods for directly computing CRC32 checksums.
The CRC32 algorithm is a commonly used error-detection mechanism for digital data. This class supports the default CRC32 polynomial and seed, with options to specify custom values. It is designed to compute CRC32 checksums for data efficiently and includes both instance-based and static methods.
Resets the hash computation to the initial state.
Processes a segment of data and updates the hash state.
Finalizes the hash computation and returns the CRC32 checksum.
Computes the CRC32 checksum for a given byte array using the default polynomial and seed.
Computes the CRC32 checksum for a given byte array using the default polynomial and a custom seed.
public static bool TryCompute(uint seed, byte[] buffer, out uint result, out string? errorMessage);
Computes the CRC32 checksum for a given byte array using a custom polynomial and seed.
byte[] data = Encoding.UTF8.GetBytes("Example data");
using var crc32 = new Crc32();
byte[] hash = crc32.ComputeHash(data);
Console.WriteLine($"CRC32 Hash: {BitConverter.ToString(hash).Replace("-", "").ToLower()}");
byte[] data = Encoding.UTF8.GetBytes("Example data");
if (Crc32.TryCompute(data, out var result, out var errorMessage)) {
Console.WriteLine($"CRC32 Checksum: {result:X}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
byte[] data = Encoding.UTF8.GetBytes("Custom polynomial example");
uint polynomial = 0x04C11DB7; // Example polynomial
uint seed = 0xFFFFFFFF;
if (Crc32.TryCompute(polynomial, seed, data, out var result, out var errorMessage)) {
Console.WriteLine($"Custom CRC32 Checksum: {result:X}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
-
Customizable:
- Supports custom polynomials and seeds for flexibility.
-
Integration:
- Extends
HashAlgorithm
, making it compatible with cryptographic workflows in .NET.
- Extends
-
Static Utility:
- Includes static methods for direct computation without creating an instance.
-
Efficiency:
- Uses a precomputed lookup table for fast CRC32 calculation.
-
Error Handling:
- Provides
TryCompute
methods with detailed error messages.
- Provides
-
Use Cases:
- Commonly used for data integrity checks in file transfers and storage.
- Can be used in networking protocols for error detection.
-
Performance:
- Optimized for performance using a lookup table.
-
Limitations:
- CRC32 is not suitable for cryptographic purposes due to its vulnerability to collisions.
This implementation provides a robust and efficient solution for CRC32 checksum computation, suitable for data integrity and verification workflows.
The JwtGenerator
class in the MaksIT.Core.Security
namespace provides methods for generating, validating, and managing JSON Web Tokens (JWTs). It supports creating tokens with claims, validating tokens, and generating secure secrets and refresh tokens.
Represents the claims of a JWT, including metadata such as username, roles, and token issuance and expiration times.
Generates a JWT with the specified claims and metadata.
if (JwtGenerator.TryGenerateToken("mysecret", "myissuer", "myaudience", 60, "user123", new List<string> { "Admin" }, out var tokenData, out var errorMessage)) {
Console.WriteLine($"Token: {tokenData?.Item1}");
Console.WriteLine($"Expires At: {tokenData?.Item2.ExpiresAt}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Generates a secure, random secret key for JWT signing.
string secret = JwtGenerator.GenerateSecret();
Console.WriteLine($"Generated Secret: {secret}");
Validates a JWT against the provided secret, issuer, and audience.
if (JwtGenerator.TryValidateToken("mysecret", "myissuer", "myaudience", "jwtTokenHere", out var claims, out var errorMessage)) {
Console.WriteLine($"Token is valid for user: {claims?.Username}");
} else {
Console.WriteLine($"Token validation failed: {errorMessage}");
}
Generates a secure, random refresh token.
string refreshToken = JwtGenerator.GenerateRefreshToken();
Console.WriteLine($"Refresh Token: {refreshToken}");
-
Token Generation:
- Create JWTs with configurable claims and expiration times.
-
Token Validation:
- Validate JWTs against the expected issuer, audience, and secret.
-
Security:
- Uses HMAC-SHA256 for signing.
- Supports generating secure secrets and refresh tokens.
-
Claims Management:
- Embed and extract custom claims such as roles and metadata.
string secret = JwtGenerator.GenerateSecret();
string issuer = "myissuer";
string audience = "myaudience";
// Generate a token
if (JwtGenerator.TryGenerateToken(secret, issuer, audience, 60, "user123", new List<string> { "Admin" }, out var tokenData, out var errorMessage)) {
Console.WriteLine($"Generated Token: {tokenData?.Item1}");
// Validate the token
if (JwtGenerator.TryValidateToken(secret, issuer, audience, tokenData?.Item1, out var claims, out var validationError)) {
Console.WriteLine($"Token valid for user: {claims?.Username}");
} else {
Console.WriteLine($"Validation failed: {validationError}");
}
} else {
Console.WriteLine($"Token generation failed: {errorMessage}");
}
-
Use Cases:
- API authentication and authorization.
- Session management with short-lived tokens and refresh tokens.
-
Best Practices:
- Store secrets securely, e.g., in a configuration service or environment variables.
- Use HTTPS to protect tokens in transit.
-
Limitations:
- Ensure token expiration times are appropriate for your security model.
This class provides a comprehensive implementation for secure JWT generation and validation, suitable for modern .NET applications.
The PasswordHasher
class in the MaksIT.Core.Security
namespace provides functionality for securely hashing passwords with salt and validating hashed passwords using the PBKDF2 (Password-Based Key Derivation Function 2) algorithm with HMAC-SHA512.
The PasswordHasher
class includes methods for:
- Generating a secure, salted hash for a password.
- Validating a password against a stored salted hash.
- Ensuring security using best practices, such as high iteration counts and fixed-time comparison.
Creates a salted hash for the given password.
if (PasswordHasher.TryCreateSaltedHash("mypassword", out var saltedHash, out var errorMessage)) {
Console.WriteLine($"Salt: {saltedHash?.Salt}");
Console.WriteLine($"Hash: {saltedHash?.Hash}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Validates a password against a stored hash and salt.
if (PasswordHasher.TryValidateHash("mypassword", saltedHash.Salt, saltedHash.Hash, out var isValid, out var errorMessage)) {
Console.WriteLine(isValid ? "Password is valid." : "Password is invalid.");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
-
Salted Hashing:
- Ensures uniqueness for each hash by combining a password with a unique salt.
-
Secure Validation:
- Uses
CryptographicOperations.FixedTimeEquals
for constant-time comparison, preventing timing attacks.
- Uses
-
High Security:
- Implements PBKDF2 with HMAC-SHA512.
- Uses 100,000 iterations for strong resistance against brute force attacks.
-
Error Handling:
- Provides detailed error messages for any failure during hashing or validation.
-
Performance:
- PBKDF2 with 100,000 iterations ensures a good balance between security and performance. The iteration count can be adjusted for specific security requirements.
-
Use Cases:
- Ideal for securely storing and validating user passwords in web applications and APIs.
-
Limitations:
- This implementation does not include password policies (e.g., minimum length, complexity), which should be enforced separately.
// Step 1: Hash a password
if (PasswordHasher.TryCreateSaltedHash("securepassword", out var saltedHash, out var errorMessage)) {
Console.WriteLine($"Salt: {saltedHash?.Salt}");
Console.WriteLine($"Hash: {saltedHash?.Hash}");
// Step 2: Validate the password
if (PasswordHasher.TryValidateHash("securepassword", saltedHash.Salt, saltedHash.Hash, out var isValid, out var validationError)) {
Console.WriteLine(isValid ? "Password is valid." : "Password is invalid.");
} else {
Console.WriteLine($"Validation Error: {validationError}");
}
} else {
Console.WriteLine($"Hashing Error: {errorMessage}");
}
-
Key Stretching:
- Use high iteration counts (e.g., 100,000 or more) to make brute force attacks computationally expensive.
-
Salt Storage:
- Store salts separately or alongside the hash in the database.
-
Encryption:
- Use a secure channel (e.g., HTTPS) when transmitting passwords to prevent interception.
The PasswordHasher
class provides a secure and efficient implementation for password hashing and validation, adhering to modern security standards.
The TotpGenerator
class in the MaksIT.Core.Security
namespace provides methods for generating and validating Time-Based One-Time Passwords (TOTP) as per the TOTP standard (RFC 6238). It also includes utility methods for generating shared secrets, recovery codes, and TOTP authentication links.
This class supports:
- TOTP generation and validation.
- Generation of base32-encoded shared secrets for TOTP.
- Recovery code generation.
- TOTP authentication link generation for integration with authenticator apps.
Validates a given TOTP code against a shared secret with time tolerance.
if (TotpGenerator.TryValidate("123456", "MZXW6YTBOI======", 1, out var isValid, out var errorMessage)) {
Console.WriteLine(isValid ? "TOTP is valid." : "TOTP is invalid.");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Generates a TOTP code for a given shared secret and time step.
if (TotpGenerator.TryGenerate("MZXW6YTBOI======", TotpGenerator.GetCurrentTimeStepNumber(), out var totpCode, out var errorMessage)) {
Console.WriteLine($"Generated TOTP: {totpCode}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Calculates the current time step number based on the current Unix timestamp.
long timestep = TotpGenerator.GetCurrentTimeStepNumber();
Console.WriteLine($"Current Time Step: {timestep}");
Generates a random shared secret encoded in base32.
if (TotpGenerator.TryGenerateSecret(out var secret, out var errorMessage)) {
Console.WriteLine($"Generated Secret: {secret}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Generates recovery codes for account recovery in case of TOTP unavailability.
if (TotpGenerator.TryGenerateRecoveryCodes(5, out var recoveryCodes, out var errorMessage)) {
Console.WriteLine("Recovery Codes:");
recoveryCodes?.ForEach(Console.WriteLine);
} else {
Console.WriteLine($"Error: {errorMessage}");
}
Generates an OTPAuth link for use with authenticator apps.
if (TotpGenerator.TryGenerateTotpAuthLink("MyApp", "user@example.com", "MZXW6YTBOI======", "MyIssuer", "SHA1", 6, 30, out var authLink, out var errorMessage)) {
Console.WriteLine($"Auth Link: {authLink}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
-
TOTP Support:
- Generate and validate TOTPs with configurable tolerance.
-
Shared Secrets:
- Generate base32-encoded secrets for use in TOTP.
-
Recovery Codes:
- Generate recovery codes for account recovery scenarios.
-
Integration:
- Create OTPAuth links for easy integration with authenticator apps.
-
Use Cases:
- Two-factor authentication (2FA) for applications.
- Secure account recovery workflows.
-
Security:
- Use HTTPS for secure transmission of secrets and TOTPs.
- Store shared secrets securely in a trusted configuration or key vault.
This class provides a robust implementation for managing TOTP workflows and integrating with authenticator apps.
This documentation provides an overview of classes in the MaksIT.Core.Webapi.Models
namespace, which are designed to facilitate Web API request and response handling. These models include functionality for pagination, filtering, patching operations, and dynamic LINQ expressions.
Represents a request model for paginated data retrieval, with support for filtering, sorting, and pagination.
var request = new PagedRequest {
PageNumber = 1,
PageSize = 10,
Filters = "Name='John' && Age>30"
};
var filterExpression = request.BuildFilterExpression<User>("Name='John' && Age>30");
Represents a response model for paginated data, including metadata about pagination and the data itself.
var response = new PagedResponse<User>(users, totalCount: 100, pageNumber: 1, pageSize: 10);
Enumerates the types of patch operations that can be performed on a field or collection.
var operation = PatchOperation.Replace;
-
Pagination:
- Simplifies paginated data handling with consistent request and response models.
-
Dynamic Filtering:
- Supports dynamic LINQ expressions for filtering datasets.
-
Partial Updates:
- Facilitates patch operations for efficient updates without replacing entire objects.
-
Extensibility:
- Designed as base models, allowing customization and extension.
var request = new PagedRequest {
PageNumber = 1,
PageSize = 10,
Filters = "Age>25 && IsActive=true"
};
// Build a LINQ expression
var filterExpression = request.BuildFilterExpression<User>("Age>25");
// Generate a paginated response
var pagedResponse = new PagedResponse<User>(users, totalCount: 100, pageNumber: 1, pageSize: 10);
Console.WriteLine($"Total Pages: {pagedResponse.TotalPages}");
var patch = new SomePatchRequestModel {
Username = "Updated Name"
Operations = new Dictionary<string, PatchOperation> {
{ "Username", PartchOperation.SetField }
}
};
// Extract operation per field
var usernmae = patch.Username;
if (TryGetOperation(nameOf(patch.Username), out operation)) {
Console.WriteLine($"Operation: {operation}, Value: {value}");
}
-
Validation:
- Validate
PagedRequest
properties to ensure proper filtering and sorting inputs.
- Validate
-
Error Handling:
- Handle edge cases, such as invalid filters or unsupported patch operations.
-
Custom Extensions:
- Extend
PagedRequest
andPagedResponse
to include additional metadata or logic as needed.
- Extend
This set of classes provides a robust framework for managing Web API requests and responses, focusing on dynamic query capabilities, efficient data handling, and flexible update mechanisms.
The Culture
class in the MaksIT.Core
namespace provides a utility method for managing and setting the culture for the current thread. This is particularly useful for applications that need to support multiple cultures or require culture-specific behavior.
The Culture
class simplifies culture management for the current thread by allowing the culture and UI culture to be set dynamically. It ensures thread safety and uses the invariant culture as a fallback when no specific culture is provided.
Sets the culture and UI culture for the current thread.
if (Culture.TrySet("fr-FR", out var errorMessage)) {
Console.WriteLine("Culture set to French (France).");
} else {
Console.WriteLine($"Failed to set culture: {errorMessage}");
}
// Set to the invariant culture
if (Culture.TrySet(null, out errorMessage)) {
Console.WriteLine("Culture set to invariant culture.");
} else {
Console.WriteLine($"Failed to set culture: {errorMessage}");
}
-
Dynamic Culture Setting:
- Allows changing the culture for the current thread dynamically.
-
Fallback to Invariant Culture:
- Automatically uses the invariant culture when no specific culture is provided.
-
Error Handling:
- Provides detailed error messages if the culture setting fails.
-
Thread Safety:
- Ensures that the culture changes only affect the current thread.
-
Use Cases:
- Localizing applications for different regions and languages.
- Ensuring consistent culture settings for thread-specific operations, such as formatting or parsing dates and numbers.
-
Error Handling:
- If an invalid culture string is provided, an exception will be caught, and the error message will describe the issue.
-
Limitations:
- This class only affects the current thread. For global culture settings, consider setting the culture for the entire application domain.
// Set culture to French (France)
if (Culture.TrySet("fr-FR", out var errorMessage)) {
Console.WriteLine("Culture set to French (France).");
Console.WriteLine($"CurrentCulture: {Thread.CurrentThread.CurrentCulture}");
Console.WriteLine($"CurrentUICulture: {Thread.CurrentThread.CurrentUICulture}");
} else {
Console.WriteLine($"Error: {errorMessage}");
}
// Attempt to set an invalid culture
if (!Culture.TrySet("invalid-culture", out errorMessage)) {
Console.WriteLine($"Expected failure: {errorMessage}");
}
// Reset to invariant culture
if (Culture.TrySet(null, out errorMessage)) {
Console.WriteLine("Culture reset to invariant culture.");
Console.WriteLine($"CurrentCulture: {Thread.CurrentThread.CurrentCulture}");
}
- Always validate input culture strings to ensure they conform to supported culture formats.
- Use this utility method in localized applications where culture settings need to be adjusted dynamically for individual threads.
This class provides a simple and effective way to manage culture settings for thread-specific operations, making it a valuable tool for applications that require localization or dynamic culture management.
The EnvVar
class in the MaksIT.Core
namespace provides utilities for managing environment variables, including adding paths to the PATH
environment variable and setting or unsetting environment variables at different scopes.
The EnvVar
class supports:
- Adding a path to the
PATH
environment variable. - Setting and unsetting environment variables for
Process
,User
, orMachine
scopes. - Cross-platform compatibility with appropriate checks for platform-specific operations.
Adds a new path to the PATH
environment variable if it is not already present.
if (EnvVar.TryAddToPath("/usr/local/bin", out var errorMessage)) {
Console.WriteLine("Path added successfully.");
} else {
Console.WriteLine($"Failed to add path: {errorMessage}");
}
Sets an environment variable at a specified scope.
if (EnvVar.TrySet("MY_ENV_VAR", "my_value", "user", out var errorMessage)) {
Console.WriteLine("Environment variable set successfully.");
} else {
Console.WriteLine($"Failed to set environment variable: {errorMessage}");
}
Unsets (removes) an environment variable at a specified scope.
if (EnvVar.TryUnSet("MY_ENV_VAR", "user", out var errorMessage)) {
Console.WriteLine("Environment variable unset successfully.");
} else {
Console.WriteLine($"Failed to unset environment variable: {errorMessage}");
}
-
Environment Variable Management:
- Supports setting, unsetting, and updating environment variables dynamically.
-
Platform Awareness:
- Handles platform-specific differences (e.g., path separators and machine-level variables on Windows only).
-
Error Handling:
- Provides descriptive error messages for failed operations.
-
Cross-Scope Support:
- Operates on
Process
,User
, orMachine
scopes, depending on requirements.
- Operates on
-
Platform-Specific Operations:
- Machine-level operations are only supported on Windows.
- Path separators (
;
or:
) are handled automatically based on the operating system.
-
Thread-Safety:
- Changes to environment variables affect the entire process and may impact other threads.
-
Use Cases:
- Dynamically configuring environment variables for applications.
- Modifying the
PATH
variable for runtime dependencies.
// Add a new path to PATH
if (EnvVar.TryAddToPath("/usr/local/bin", out var pathError)) {
Console.WriteLine("Path added to PATH successfully.");
} else {
Console.WriteLine($"Failed to add path: {pathError}");
}
// Set an environment variable
if (EnvVar.TrySet("MY_ENV_VAR", "value123", "user", out var setError)) {
Console.WriteLine("Environment variable set successfully.");
} else {
Console.WriteLine($"Failed to set variable: {setError}");
}
// Unset an environment variable
if (EnvVar.TryUnSet("MY_ENV_VAR", "user", out var unsetError)) {
Console.WriteLine("Environment variable unset successfully.");
} else {
Console.WriteLine($"Failed to unset variable: {unsetError}");
}
- Use process-level variables for temporary configurations that do not require persistence.
- Use user-level variables for configurations specific to a user account.
- Avoid modifying machine-level variables unless necessary, and always test the impact on the system.
This class provides a convenient and platform-aware way to manage environment variables, making it suitable for dynamic runtime configurations.
The FileSystem
class in the MaksIT.Core
namespace provides utility methods for working with the file system, including file and folder operations, resolving wildcard paths, and handling duplicate file names.
The FileSystem
class supports:
- Copying files and folders to a target directory.
- Deleting files and directories.
- Resolving paths with wildcards.
- Handling file name duplication by appending a counter.
- Ensures cross-platform compatibility with platform-specific handling for Windows and other OSes.
Copies a file or the contents of a folder to a specified destination folder.
if (FileSystem.TryCopyToFolder("sourcePath", "destinationPath", true, out var errorMessage)) {
Console.WriteLine("Copy operation successful.");
} else {
Console.WriteLine($"Copy operation failed: {errorMessage}");
}
Deletes a file or directory at the specified path.
if (FileSystem.TryDeleteFileOrDirectory("pathToItem", out var errorMessage)) {
Console.WriteLine("Delete operation successful.");
} else {
Console.WriteLine($"Delete operation failed: {errorMessage}");
}
Resolves paths with wildcards and returns all matching paths.
var resolvedPaths = FileSystem.ResolveWildcardedPath("?:\\Users\\*\\AppData\\Roaming\\*");
resolvedPaths.ForEach(Console.WriteLine);
Checks for file name duplication and appends a counter if a duplicate is found.
var uniqueFilePath = FileSystem.DuplicateFileNameCheck("path/to/file.txt");
Console.WriteLine($"Unique file path: {uniqueFilePath}");
-
File and Folder Operations:
- Copy files or entire folders with an option to overwrite existing files.
- Delete files and directories, including recursive deletion for directories.
-
Wildcard Path Resolution:
- Resolve and return all matching paths for wildcard-based patterns.
-
Duplicate File Name Handling:
- Automatically appends a counter to duplicate file names to ensure uniqueness.
-
Cross-Platform Support:
- Handles platform-specific path separators and behaviors.
-
Platform Awareness:
- Uses
RuntimeInformation
to differentiate between Windows and non-Windows environments for platform-specific operations.
- Uses
-
Error Handling:
- All methods provide detailed error messages for failed operations.
-
Use Cases:
- Managing file and folder content dynamically.
- Handling file system paths with wildcard patterns.
- Avoiding file name collisions during file operations.
// Example 1: Copy a folder
if (FileSystem.TryCopyToFolder("sourceFolderPath", "destinationFolderPath", true, out var copyError)) {
Console.WriteLine("Folder copied successfully.");
} else {
Console.WriteLine($"Error copying folder: {copyError}");
}
// Example 2: Delete a file or folder
if (FileSystem.TryDeleteFileOrDirectory("pathToDelete", out var deleteError)) {
Console.WriteLine("Item deleted successfully.");
} else {
Console.WriteLine($"Error deleting item: {deleteError}");
}
// Example 3: Resolve wildcard paths
var paths = FileSystem.ResolveWildcardedPath("?:\\Users\\*\\Documents\\*");
paths.ForEach(Console.WriteLine);
// Example 4: Handle duplicate file names
var uniqueFile = FileSystem.DuplicateFileNameCheck("path/to/myFile.txt");
Console.WriteLine($"Unique file path: {uniqueFile}");
- Ensure proper permissions for file and directory operations.
- Handle exceptions for inaccessible paths when using wildcard resolutions.
- Use
DuplicateFileNameCheck
to prevent overwriting existing files unintentionally.
The FileSystem
class provides a comprehensive set of utilities for dynamic file system operations, making it ideal for applications requiring flexible and robust file management capabilities.
The Processes
class in the MaksIT.Core
namespace provides helper methods to manage system processes, allowing you to start new processes or kill existing ones by name.
The Processes
class supports:
- Starting new processes with optional arguments and timeout handling.
- Killing processes by name, with support for wildcard matching (
*
and?
).
- Start Process: Launch a process with customizable settings.
- Kill Process: Terminate processes by name, including wildcard support.
Starts a new process with specified arguments and settings.
if (Processes.TryStart("notepad.exe", "", 0, false, out var errorMessage)) {
Console.WriteLine("Process started successfully.");
} else {
Console.WriteLine($"Failed to start process: {errorMessage}");
}
Terminates processes matching the specified name.
if (Processes.TryKill("notepad*", out var errorMessage)) {
Console.WriteLine("Processes killed successfully.");
} else {
Console.WriteLine($"Failed to kill processes: {errorMessage}");
}
-
Process Management:
- Start new processes with optional timeout and window visibility settings.
- Kill processes by name, with wildcard matching for flexible targeting.
-
Error Handling:
- Provides detailed error messages for any failed operations.
-
Wildcard Support:
- Matches process names using patterns (
*
and?
) to handle dynamic process names.
- Matches process names using patterns (
-
Customizable Execution:
- Configure process settings such as visibility (
silent
) and wait time (timeout
).
- Configure process settings such as visibility (
-
Thread-Safety:
- Process management may affect other threads in the application.
-
Platform-Specific Behavior:
- Works on all platforms supported by .NET, but behavior may vary depending on the operating system.
-
Use Cases:
- Automating the launch of system tools or scripts.
- Cleaning up orphaned or unnecessary processes in an automated environment.
// Example 1: Start a process
if (Processes.TryStart("notepad.exe", "", 10, false, out var startError)) {
Console.WriteLine("Notepad started successfully.");
} else {
Console.WriteLine($"Error starting Notepad: {startError}");
}
// Example 2: Kill processes matching a pattern
if (Processes.TryKill("notepad*", out var killError)) {
Console.WriteLine("Notepad processes killed successfully.");
} else {
Console.WriteLine($"Error killing Notepad processes: {killError}");
}
-
Error Handling:
- Always check the return value and error messages to handle exceptions gracefully.
-
Timeout Configuration:
- Use a reasonable timeout when waiting for a process to exit to avoid indefinite blocking.
-
Wildcard Patterns:
- Use precise patterns to avoid unintended process termination when using
TryKill
.
- Use precise patterns to avoid unintended process termination when using
-
Permission Handling:
- Ensure the application has appropriate permissions to start or terminate processes.
The Processes
class provides a robust utility for managing processes in .NET applications, suitable for both user-level and system-level automation tasks.