My guess is that you are opening the file twice in your second piece of code:
using (var sw = File.AppendText(filePath + fileName))
{
sw.Write(ctx.GetMD5HashFromFile(filePath, fileName));
}
ctx.GetMD5HashFromFile
probably opens the file to create the hash; but you already opened it to append data. So create the hash before the File.AppendText
using
block.