Calculate MD5 checksum for a file

It’s very simple using System.Security.Cryptography.MD5: using (var md5 = MD5.Create()) { using (var stream = File.OpenRead(filename)) { return md5.ComputeHash(stream); } } (I believe that actually the MD5 implementation used doesn’t need to be disposed, but I’d probably still do so anyway.) How you compare the results afterwards is up to you; you can convert the … Read more

What is a method group in C#?

A method group is the name for a set of methods (that might be just one) – i.e. in theory the ToString method may have multiple overloads (plus any extension methods): ToString(), ToString(string format), etc – hence ToString by itself is a “method group”. It can usually convert a method group to a (typed) delegate … Read more

Can a Byte[] Array be written to a file in C#?

Based on the first sentence of the question: “I’m trying to write out a Byte[] array representing a complete file to a file.” The path of least resistance would be: File.WriteAllBytes(string path, byte[] bytes) Documented here: System.IO.File.WriteAllBytes – MSDN

How to truncate milliseconds off of a .NET DateTime

The following will work for a DateTime that has fractional milliseconds, and also preserves the Kind property (Local, Utc or Undefined). DateTime dateTime = … anything … dateTime = new DateTime( dateTime.Ticks – (dateTime.Ticks % TimeSpan.TicksPerSecond), dateTime.Kind ); or the equivalent and shorter: dateTime = dateTime.AddTicks( – (dateTime.Ticks % TimeSpan.TicksPerSecond)); This could be generalized into … Read more

Which .NET Dependency Injection frameworks are worth looking into? [closed]

edit (not by the author): There is a comprehensive list of IoC frameworks available at https://github.com/quozd/awesome-dotnet/blob/master/README.md#ioc: Castle Windsor – Castle Windsor is best of breed, mature Inversion of Control container available for .NET and Silverlight Unity – Lightweight extensible dependency injection container with support for constructor, property, and method call injection Autofac – An addictive … Read more

Difference in months between two dates

Assuming the day of the month is irrelevant (i.e. the diff between 2011.1.1 and 2010.12.31 is 1), with date1 > date2 giving a positive value and date2 > date1 a negative value ((date1.Year – date2.Year) * 12) + date1.Month – date2.Month Or, assuming you want an approximate number of ‘average months’ between the two dates, … Read more

Total number of items defined in an enum

You can use the static method Enum.GetNames which returns an array representing the names of all the items in the enum. The length property of this array equals the number of items defined in the enum var myEnumMemberCount = Enum.GetNames(typeof(MyEnum)).Length;

Getting full URL of action in ASP.NET MVC [duplicate]

There is an overload of Url.Action that takes your desired protocol (e.g. http, https) as an argument – if you specify this, you get a fully qualified URL. Here’s an example that uses the protocol of the current request in an action method: var fullUrl = this.Url.Action(“Edit”, “Posts”, new { id = 5 }, this.Request.Url.Scheme); … Read more

Do zombies exist … in .NET?

Is there a clearer definition of a “zombie thread” than what I’ve explained here? Seems like a pretty good explanation to me – a thread that has terminated (and can therefore no longer release any resources), but whose resources (e.g. handles) are still around and (potentially) causing problems. Can zombie threads occur on .NET? (Why/Why … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)