C# Programming > Data

C# Self Hashing Application

Calculating Hash Code

Calculating the hash code of a file using algorithms such as MD5 and SHA1 is not too difficult.

But what about an application that computes its own hash code? The tricky part is reading the application file while it is in use.

Open File

To calculate the hash value of the running application the program needs to read its own executing file. However since the file is in use, some methods to read the file will give an error like this:

The process cannot access the file '[file path]' because it is being used by another process.

We can avoid this type of error by using the proper .NET classes to read an open file. For example, the System.IO.File class has a static function OpenRead which returns a stream to read the content of the files.

With OpenRead a C# developer can extract the byte data of any file, including the one of the running application.

Sample Application

The best way to understand the concept is to see the application in action. The sample application displays its own MD5 hash code.

Back to C# Article List