FileInfo! Lock Files

Imagine a winform application that copies updated assemblies from source folder A to destination folder B. I use the simple methods DirectoryInfo.GetFiles to populate the list by comparing the version of the assembly in folder A and B; if some builds are newer, I start my update method. In this method, before copying, I try if all the files in folder B are not used:

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, due to the previous UpdateListView code, which uses FileInfo to get information to show all my files!

FileInfo! Lock Files Is it possible?

Can anyone suggest a way around this problem?

Thanks Nando

+3
source share
1 answer

, File.Open .

:

using(var file = File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
   // process here
}
+4

Source: https://habr.com/ru/post/1761735/


All Articles