Problem Statement: Extracting extended file properties, such as the Media Created Date, is necessary for certain applications. Utilizing the Microsoft.WindowsAPICodePack.Shell namespace facilitates this task efficiently.
Approach: Utilize the ShellObject class from the WindowsAPICodePack.Shell namespace to access extended file properties. Specifically, retrieve the Media Created Date using the System.Media.DateEncoded property.
using Microsoft.WindowsAPICodePack.Shell;
ShellObject shell = ShellObject.FromParsingName(path);
var mediaCreatedDate = shell.Properties.System.Media.DateEncoded;
Explanation: This code utilizes the ShellObject class to access extended file properties. It fetches the Media Created Date of a file specified by its path using the System.Media.DateEncoded property, providing crucial metadata for various applications.
Further resources
C# is a strongly-typed language. It means we must declare the type of a variable that indicates the kind of values it is going to store, such as integer, float, decimal, text,
Additional reading at Data types in C# (tutorialsteacher.com)
C# includes specialized classes that store series of values or objects are called collections.
There are two types of collections available in C#: non-generic collections and generic collections.
The System.Collections
namespace contains the non-generic collection types and System.Collections.Generic
namespace includes generic collection types.
Additional reading at C# Generic & Non-generic Collections (tutorialsteacher.com)
In C#, a string is a series of characters that is used to represent text. It can be a character, a word or a long passage surrounded with the double quotes ". The following are string literals.
// String Literal Examples
"S"
"String"
"This is a string."
To filter out nullable strings and obtain only non-null strings, you can use LINQ Where
method along with a null check. Here's an example:
IEnumerable<string?> nullableStrings //
List<string> nonNullStringsList = nullableStrings
.Where(s => s != null) // Filter out null values
.Select(s => s!) // Convert nullable strings to non-nullable
.ToList(); // Convert IEnumerable to List
Where
to filter out null values and Select
to convert nullable strings to non-nullable strings (string!
indicates a non-nullable reference type in C# 8.0 and later).ToList()
to convert the filtered enumerable to a List<string>
.The JsonSerializer.Serialize
converts the value of a specified type into a JSON string.
using System.Text.Json;
var user = new User("Krish Jackon", "female", new MyDate(1985, 03, 30));
var json = JsonSerializer.Serialize(user);
Console.WriteLine(json);
The JsonSerializer.Deserialize
parses the text representing a single JSON value into an instance of a specified type.
using System.Text.Json;
string json = @"{""Name"":""Krish Jackon"", ""Gender"":""female"",
""DateOfBirth"":{""year"":1985,""month"":03,""day"":30}}";
var user = JsonSerializer.Deserialize<User>(json);
Resources to deserialize to dynamic object
In C#, to compare two strings ignoring case sensitivity, you can use the string.Equals method with StringComparison.OrdinalIgnoreCase as shown below:
string val = "aStringValue";
bool isEqual = string.Equals(val, "astringvalue", StringComparison.OrdinalIgnoreCase);
This code snippet compares the val
string with "astringvalue" regardless of case sensitivity and returns a boolean indicating if they are equal.
For more details and discussions about comparing strings in C# while ignoring case, you can refer to this Stack Overflow thread: Comparing two strings ignoring case in C#.
The provided source code defines a simple DependencyProvider class responsible for managing dependencies using .NET Core's built-in dependency injection system. The class includes a method to retrieve required services based on their interfaces.
public static class DependencyProvider
{
private static IServiceProvider Provider()
{
var services = new ServicesCollection();
services.AddSingleton<IClient, Client>();
return services.BuildServiceProvider();
}
public static T GetRequiredService<T>() where T : notnull
{
var provider = Provider();
return provider.GetRequiredService<T>();
}
}
To use this DependencyProvider, you can retrieve instances of required services as shown below:
readonly IClient _client = DependencyProvider.GetRequiredService<IClient>();
To add IConfiguration
to the service collection, you can use the IServiceCollection
interface. First, create a ServiceCollection
instance, then use the AddSingleton
method to add IConfiguration
, passing in your configuration object _config
.
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json");
IConfiguration configuration = builder.Build();
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IConfiguration>(configuration);
This setup is particularly useful for scenarios like unit testing background services, as discussed in this Stack Overflow thread: Unit testing a .NET Core background service.