-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (68 loc) · 2.7 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(string[] args)
{
var path = "/Volumes/Data-1/test/test"; // Updated SMB share path
Console.WriteLine($"Monitoring folder: {path}. Press any key to exit...");
var activeFiles = new Dictionary<string, long>();
var completedFiles = new HashSet<string>();
while (true)
{
var currentFiles = Directory.GetFiles(path);
foreach (var file in currentFiles)
{
if (completedFiles.Contains(file))
{
continue; // Skip files that are already completed
}
long currentSize;
try
{
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
currentSize = stream.Length;
Console.WriteLine($"Current size of {file} is {currentSize}");
}
}
catch (IOException)
{
// Failed to open the file; likely still being written to
continue;
}
if (!activeFiles.ContainsKey(file))
{
activeFiles[file] = currentSize;
Console.WriteLine($"File created: {file}");
continue;
}
if (currentSize == 0 || currentSize != activeFiles[file])
{
activeFiles[file] = currentSize;
}
else
{
// Try to acquire a lock on the file
try
{
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
{
// If we can acquire a lock, the file is not being written to anymore
Console.WriteLine($"File finished copying: {file}");
completedFiles.Add(file);
activeFiles.Remove(file); // Remove the file from the active files dictionary
}
}
catch (IOException)
{
// Failed to acquire a lock; file is still being written to
continue;
}
}
}
System.Threading.Thread.Sleep(2000); // Wait for 2 seconds before checking again
}
}
}