dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

Using the FileSystemWatcher control on 2 different PCs using a common folder. #77575

Open Perpete opened 2 years ago

Perpete commented 2 years ago

.NET version

.NET 6 project in VB with WPF or Winform and visual studio 17.3.6.

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

I don't know.

Issue description

Hello,

An identical program is used on 2 computers. Each program on its computer can create and delete the same file on the cloud. The cloud is seen by the 2 computers. This program uses the FileSystemWatcher control to monitor file erasure from the cloud.

When, I create and delete the file with one of the 2 programs, the FileSystemWatcher control sees the deletion of the file on the program that deleted the file. In contrast, the other program's FileSystemWatcher control does not see the file deletion. Yet it monitors the same directory and the same file. Here is a screenshot of the 2 computers.

File in common cloud directory.

DirCloud

PC1:

PC1

PC2:

PC2

Here is the program used on each computer in VB.

`Imports System.IO Imports System.Windows Public Class Form1

Const pathTest As String = "P:\Essai"
Const fileTest As String = "Essai.txt"

Dim watcher As New FileSystemWatcher(pathTest)

Private Sub btnCreateFile_Click(sender As Object, e As EventArgs) Handles btnCreateFile.Click

    Dim sw As StreamWriter

    'Crée le fichier
    If File.Exists(pathTest & "\" & fileTest) = False Then
        sw = File.CreateText(pathTest & "\" & fileTest)
        sw.Flush()
        sw.Close()

        txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - file creation" & vbCrLf
    Else
        txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - the file already exists" & vbCrLf
    End If

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'Nom du fichier à surveiller
    watcher.Filter = fileTest

    'Ajoute l'événement de suppression de fichier

    AddHandler watcher.Deleted, AddressOf OnDeleted

    watcher.SynchronizingObject = Me
    watcher.EnableRaisingEvents = True

End Sub

Private Sub OnDeleted(sender As Object, e As FileSystemEventArgs)

    'Gestion de la suppression du fichier

    txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - detection Of file deletion (Filesystemwatcher)" & vbCrLf

End Sub

Private Sub btnClearInfo_Click(sender As Object, e As EventArgs) Handles btnClearInfo.Click

    'Suppresion des infos
    txtInfos.Clear()

End Sub

Private Sub btnDeleteFile_Click(sender As Object, e As EventArgs) Handles btnDeleteFile.Click

    'Efface le fichier
    File.Delete(pathTest & "\" & fileTest)

    txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - file deletion" & vbCrLf

End Sub

End Class`

Here is my test program.

WinFormsApp1.zip

Steps to reproduce

Use the test program on 2 different computers with a common folder.

Perpete commented 2 years ago

Hello,

Running the program twice and at the same time on a single computer does not cause this problem.

On the screenshot, we see the detection of the deletion of the file by the FileSystemWatcher controls of the 2 programs.

PCseul

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

Issue Details
### .NET version .NET 6 project in VB with WPF or Winform and visual studio 17.3.6. ### Did it work in .NET Framework? No ### Did it work in any of the earlier releases of .NET Core or .NET 5+? I don't know. ### Issue description Hello, An identical program is used on 2 computers. Each program on its computer can create and delete the same file on the cloud. The cloud is seen by the 2 computers. This program uses the FileSystemWatcher control to monitor file erasure from the cloud. When, I create and delete the file with one of the 2 programs, the FileSystemWatcher control sees the deletion of the file on the program that deleted the file. In contrast, the other program's FileSystemWatcher control does not see the file deletion. Yet it monitors the same directory and the same file. Here is a screenshot of the 2 computers. File in common cloud directory. ![DirCloud](https://user-images.githubusercontent.com/66659162/198288281-b56ff3b2-f31b-487d-8787-233b590ab3dd.png) PC1: - File creation - **No detection of deletion of file on PC2 at 02:25:27 09 by PC1's FileSystemWatcher control** ![PC1](https://user-images.githubusercontent.com/66659162/198286684-31c70e27-a24c-43f6-b5f3-f67aea9a1f72.png) PC2: - File deletion - Detection of deletion by the FileSystemWatcher control ![PC2](https://user-images.githubusercontent.com/66659162/198286992-99927e28-7336-40f2-a293-b2772a5ee6a4.png) Here is the program used on each computer in VB. `Imports System.IO Imports System.Windows Public Class Form1 Const pathTest As String = "P:\Essai" Const fileTest As String = "Essai.txt" Dim watcher As New FileSystemWatcher(pathTest) Private Sub btnCreateFile_Click(sender As Object, e As EventArgs) Handles btnCreateFile.Click Dim sw As StreamWriter 'Crée le fichier If File.Exists(pathTest & "\" & fileTest) = False Then sw = File.CreateText(pathTest & "\" & fileTest) sw.Flush() sw.Close() txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - file creation" & vbCrLf Else txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - the file already exists" & vbCrLf End If End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Nom du fichier à surveiller watcher.Filter = fileTest 'Ajoute l'événement de suppression de fichier AddHandler watcher.Deleted, AddressOf OnDeleted watcher.SynchronizingObject = Me watcher.EnableRaisingEvents = True End Sub Private Sub OnDeleted(sender As Object, e As FileSystemEventArgs) 'Gestion de la suppression du fichier txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - detection Of file deletion (Filesystemwatcher)" & vbCrLf End Sub Private Sub btnClearInfo_Click(sender As Object, e As EventArgs) Handles btnClearInfo.Click 'Suppresion des infos txtInfos.Clear() End Sub Private Sub btnDeleteFile_Click(sender As Object, e As EventArgs) Handles btnDeleteFile.Click 'Efface le fichier File.Delete(pathTest & "\" & fileTest) txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - file deletion" & vbCrLf End Sub End Class` Here is my test program. [WinFormsApp1.zip](https://github.com/dotnet/winforms/files/9879513/WinFormsApp1.zip) ### Steps to reproduce Use the test program on 2 different computers with a common folder.
Author: Perpete
Assignees: -
Labels: `area-System.IO`, `untriaged`
Milestone: -
adamsitnik commented 2 years ago

Hi @Perpete

What do you mean by cloud? What kind of file share is it? I am asking because we will need to setup something similar to try to reproduce and investigate the issue. Thanks!

ghost commented 2 years ago

This issue has been marked needs-author-action and may be missing some important information.

Perpete commented 2 years ago

Hello,

I use pCloud which is an online storage service. After installing the program, it creates pcloud Drive (P:). The program is installed on the 2 computers with the same settings. The "P" drive is then common to the 2 computers.

pCloud

For a test, I used a Timer control. In its "Tick" event, I check for file deletion with the "File.Exists(xxxx)" method. The detection of file deletion using the Timer works perfectly on both computers.

PC1_Timer

PC2_Timer

The 2 programs see the deletion of the file using the timer.

Here is part of the code with the timer added.

`

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'Nom du fichier à surveiller
    watcher.Filter = fileTest

    'Ajoute l'événement de suppression de fichier
    AddHandler watcher.Deleted, AddressOf OnDeleted

    watcher.SynchronizingObject = Me
    watcher.EnableRaisingEvents = True

    'Timer
    tmrDelete.Interval = 500
    tmrCreatFile.Interval = 500
    tmrCreatFile.Start()

End Sub

Private Sub tmrDelete_Tick(sender As Object, e As EventArgs) Handles tmrDelete.Tick

    'Gestion de la suppression du fichier
    If File.Exists(pathTest & "\" & fileTest) = False Then
        tmrDelete.Stop()
        txtInfos.Text = txtInfos.Text & Format(Now, "hh:mm:ss ff") & " - detection Of file deletion (Timer)" & vbCrLf
    End If

End Sub

Private Sub tmrCreatFile_Tick(sender As Object, e As EventArgs) Handles tmrCreatFile.Tick

    'Vérifie la création du fichier
    If File.Exists(pathTest & "\" & fileTest) = True AndAlso tmrDelete.Enabled = False Then
        tmrDelete.Start()
    End If

End Sub

`