Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My C# app is locking a file, how I can find where it does it?

I'm writing code that check files path calculate hash (SHA1) and copy them. I made sure that I do not lock them like for example using

public static string SHA1(string filePath)
    {
        var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var formatted = string.Empty;
        using (var sha1 = new SHA1Managed())
        {
            byte[] hash = sha1.ComputeHash(fs);
            foreach (byte b in hash)
            {
                formatted += b.ToString("X2");
            }
        }
        return formatted;
    }

So how I can, in Visual Studio, find where it does lock the file?

like image 201
Data-Base Avatar asked Nov 28 '22 19:11

Data-Base


2 Answers

Can you keep the above syntax as and give a try?

using(var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
//Your code goes here.
} 
like image 106
Siva Gopal Avatar answered Dec 04 '22 23:12

Siva Gopal


There is a little windows soft : process explorer and in this you can find which process has an handle on a file :

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

like image 45
remi bourgarel Avatar answered Dec 04 '22 23:12

remi bourgarel