I use the FileSystemWatcher in my C# application and this application runs as a Windows Service on my server. The watchers path is a Samba share from another network. The application works totally fine when the folder path of the watcher is a folder on the Server, but with the shared folder no events are raised - I need to be informed when a file is created in the folder, then I move it to another one, rename it, read it and so on. (I also tried the watcher.Changed event but nothing is happening there either)
I found a similar question here: FileSystemWatcher with Samba on Linux
Does anybody know if the FSW has still problems with Samba-shared folders? I already tried to use a StreamReader and StreamWriter to test if I even have access to the shared folder - this works without any problems. I also thought about resetting the EnableRaisingEvents to true if the FSW "breaks" (like it is mentioned in the question above) but I am a bit confused how to even find out if it broke - because I don't get an error, it just does nothing at all.
This is a part of my watcher class, it runs as a BackgroundService:
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Initialize();
TestAccessability();
RunFileWatcher();
return Task.CompletedTask;
}
private void TestAccessability()
{
// Get the directories currently on the shared drive.
DirectoryInfo[] sDirs = new DirectoryInfo(@"\\10.18.249.8\halit4ind$").GetDirectories();
// Write each directory name to a file.
using (StreamWriter sw = new StreamWriter(importPath + "\\SDriveDirs.txt"))
{
foreach (DirectoryInfo dir in sDirs)
{
sw.WriteLine(dir.Name);
}
}
// Read and show each line from the file.
string line = "";
using (StreamReader sr = new StreamReader(importPath + "\\SDriveDirs.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
private void RunFileWatcher()
{
logger.LogInformation($"RunFileWatcher watching path {importPath}");
watcher = new FileSystemWatcher
{
Path = @importPath,
Filter = "*.csv"
};
watcher.Created += OnCreated;
watcher.Changed += OnCreated;
watcher.EnableRaisingEvents = true;
}
private void OnCreated(object source, FileSystemEventArgs e)
{
logger.LogInformation($"File {e.FullPath} created/changed - Type: {e.ChangeType}");
if (e.ChangeType == WatcherChangeTypes.Created)
{
var newFilename = TryMoveFileToWork(e.Name);
MoveFileToArchiv(newFilename);
}
}
This is the output I get in the console (I tried creating a file in \10.18.249.8\halit4ind$\Outbox but nothing happens):
pers
Inbox
Outbox
Work
Archiv
[07:19:57 INF] RunFileWatcher watching path \\10.18.249.8\halit4ind$\Outbox
[07:19:57 DBG] Failed to locate the development https certificate at 'null'.
[07:19:57 DBG] Hosting started
[07:19:57 DBG] Loaded hosting startup assembly InfoniqaServiceHali
Hosting environment: Production
Content root path: D:\Services\InfoniqaServiceHali
Now listening on: http://0.0.0.0:7040
Application started. Press Ctrl+C to shut down.
This is the output I get if I use another path:
pers
Inbox
Outbox
Work
Archiv
[08:49:35 INF] RunFileWatcher watching path D:\temp\Outbox
[08:49:35 DBG] Failed to locate the development https certificate at 'null'.
[08:49:35 DBG] Hosting started
[08:49:35 DBG] Loaded hosting startup assembly InfoniqaServiceHali
Hosting environment: Production
Content root path: D:\Services\InfoniqaServiceHali
Now listening on: http://0.0.0.0:7040
Application started. Press Ctrl+C to shut down.
[08:49:44 INF] File D:\temp\Outbox\Personal.csv created/changed - Type: Created
[08:49:44 INF] TryMoveFileToWork Personal.csv