Suppose someone copies files to the server in the place "B" from outside by some file manager.

How to understand if the file in the "B" place is completely copied and if so start processing it?

  • one
    Well, this is for you to dig in the direction of FileSystemWatcher. And if he suddenly does not give the necessary information that the file change is over, you will have to try to open the file before the victory. Until his copier lets go, the Windows will still not give him access. - vitidev

1 answer 1

The code is working, checked by downloading the Debian OS image via wget:

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace BusyFileObserver { class Program { private static bool isBusyFile(FileInfo fileInfo) { FileStream stream = null; try { stream = fileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { return true; } finally { if (stream != null) stream.Close(); } return false; } static void Main(string[] args) { string pathToFile = @"C:\Users\isnullxbh\Downloads\debian-distro.iso"; FileInfo fileInfo = new FileInfo(pathToFile); while (isBusyFile(fileInfo)) { Console.WriteLine("\nisBusy..."); Thread.Sleep(2000); } System.Diagnostics.Process.Start(pathToFile); } } }