Question

Imagine a winform app, who copy updated assemblies from a source folder A to a destination folder B. I use simple DirectoryInfo.GetFiles methods to fill a listview, comparing version of assembly in folder A and B; if some assemblies are newer, I start my update method. In this method, before copying, I try if all file in B folder are not in use:

var B = new DirectoryInfo("myBfolder");
foreach (var file in aFolder.GetFiles())
{
    try
    {
        //File not in use
        File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (Exception ex)
    {
        //File in use!
    }
}

Well, because of previous UpdateListView code, that use FileInfo to get info to show, all my files results in use!

FileInfo lock files! Is this possible?

Can someone suggest a way to bypass this problem?

Thank you, Nando

Was it helpful?

Solution

no, it is File.Open who lock files.

try to put it into using:

using(var file = File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
   // process here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top