Sandy
Sandy

Reputation: 11717

Window Service when folder copying is finished

I have a server location path like \\10.100.100.10000\Builds. Whenever somebody fires a force build in our server in Visual Studio, a new build folder, like 2013.1.3.1, gets created and new files are copied into it. I need to write a Windows Service so when the folder gets copied completely, I need to extract Application.exe and do some processing.

My question is how to know if the folder has been copied completely? Googling and Binging did not help me much. Do I have to tell my team who is firing the build event to do some custom work to let my program know that the copying files have finished? Or there is some .Net libraries to accomplish this task?

Upvotes: 1

Views: 993

Answers (2)

Sandy
Sandy

Reputation: 11717

This is how I did it in the end to see if a folder copying has been finished. Create a System.IO.FileSystemWatcher and assign a event at Created property.

private void FileSystemWatcherCreated(object sender, FileSystemEventArgs e)
{
    long sizeOld = GetDirectorySize(new DirectoryInfo(e.FullPath));

    Thread.Sleep(100000);

    long sizeNew = GetDirectorySize(new DirectoryInfo(e.FullPath));

    if (sizeOld == sizeNew)
    {
        // Copying finished.
    }
}

The above method just checks the folder size every one minute and then compare the sizes. If sizes are same, declares that copying folder has finished.

Hope it helps.

Upvotes: 1

Kokulan Eswaranathan
Kokulan Eswaranathan

Reputation: 165

You could use a FileSystemWatcher to watch for the activity in the folder, the monitoring could be started when the Windows Service starts (In Start Method).

You could have the following event handlers registered with the FileWatcher

FSWatcher.Deleted += new System.IO.FileSystemEventHandler(this.FSWatcher_Deleted); FSWatcher.Renamed += new System.IO.RenamedEventHandler(this.FSWatcher_Renamed); FSWatcher.Changed += new System.IO.FileSystemEventHandler(this.FSWatcher_Changed); FSWatcher.Created += new System.IO.FileSystemEventHandler(this.FSWatcher_Created);

FSWatcher_Changed event will be kept fired until the copy process finishes.

You could use this event to know when the copy started and wait till the copy process finishes.

Once the copy process is finished, I mean when the folder is quiet, you could start doing the extracting of Application.Exe or any other tasks you would like to do.

Hope it helps.

Upvotes: 1

Related Questions