Handlebars is a fantastic templating engine which is available for .NET. It’s similar to Mustache, which I tend to prefer, but Handlebars has more features that can be very useful. This is a basic example showing how easy it is to apply a template. // <package id=”Handlebars.Net” version=”1.7.1″ targetFramework=”net40″ /> // {{ “{{” }}{ triple-stash to […]

Get EXIF date from photo

It’s not straightforward to retrieve the EXIF date from an image. Here’s a method to retrieve it using the MetadataExtractor package. It looks for the appropriate date, and if not found, falls back to the file properties. // <package id=”MetadataExtractor” version=”2.0.0″ targetFramework=”net40″ /> static DateTime GetImageDate(string file) { var directories = ImageMetadataReader.ReadMetadata(file); ExifSubIfdDirectory subIfdDirectory = […]

Configuration in .NET Core

Here’s some basic examples for reading from a configuration file: 1) Create an appsettings.json file in your project. 2) Add this to your csproj file, so that it gets to your output directory: <ItemGroup> <None Update=”appsettings.json”> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None></ItemGroup> 3) Install the appropriate NuGet packages: dotnet add package Microsoft.Extensions.Configuration.Jsondotnet add package Microsoft.Extensions.Configuration.Binder 4) Add data to […]

Add EXIF date to photo

If you have a photo that you need to add a timestamp to the EXIF information, you can use the following .NET code. Apparently there are a ridiculous number of formats and standards which are incompatible with each other and conflicting, but from what I’ve found, this should cover regular use cases. I’m sure there’s […]

ASP.NET Embedded Resource

You can compile binary or text files into the application assembly – here’s the basic idea. Add the file to the project in Visual Studio, and in the Properties, mark it as “Embedded Resource”. The name of the resource is the default namespace for the project + relative namespace (folder structure) + filename. This is […]

SQL Table Valued Parameters in .NET

private static SqlDataRecord CreatePersonRecord(int id, string name) { var metaData = new[] { new SqlMetaData(“Id”, SqlDbType.Int), new SqlMetaData(“Name”, SqlDbType.VarChar, 50) }; var record = new SqlDataRecord(metaData); record.SetInt32(0, id); record.SetString(1, name); return record;}private SqlDataRecord[] GetPersonRecords() { retun new[] { CreatePersonRecord(1, “John Doe”), CreatePersonRecord(2, “Jane Doe”) };}using (var conn = new SqlConnection(cs)) { conn.Open(); using (var comm […]

Resize image in .NET

This will resize image to be 1024 height or width (whichever is bigger). Explore other options to look for things like image quality. using (var image = Image.FromFile(file.FullName)){ int newHeight, newWidth; if (image.Height > image.Width) { newHeight = 1024; newWidth = (int)(1024.0 * image.Width / image.Height); } else { newWidth = 1024; newHeight = (int)(1024.0 […]

Demonstrating a SQL deadlock with .NET

  var conn1 = new SqlConnection(cs); conn1.Open(); var comm1 = conn1.CreateCommand(); comm1.CommandText = “Proc1;”; new Thread(() => { try { Console.WriteLine(“Executing comm1”); comm1.ExecuteNonQuery(); Console.WriteLine(“comm1 done”); } catch (Exception ex) { Console.WriteLine(“comm1: ” + ex); } }).Start(); var conn2 = new SqlConnection(cs); conn2.Open(); var comm2 = conn2.CreateCommand(); comm2.CommandText = “Proc2”; new Thread(() => { try { […]

Async/await in console app

// PM> Install-Package Nito.AsyncEx using Nito.AsyncEx; class Program { static void Main(string[] args) { AsyncContext.Run(() => MainAsync(args)); } static async void MainAsync(string[] args) { // await Something… }} This was an answer on StackOverflow by async/await superstar and blogger Stephen Cleary, who wrote the referenced NuGet package.

C# Top level statements

In the newest version of C#, you can have top-level statements instead of putting your entry point in a static void Main method. This saves you a couple of indents for your application’s starting point, which doesn’t really belong in a class in the first place. The compiler magically puts it in a special class […]