Search code examples
c#phplinuxtext-filestapi

C# doesn't detect text file that was edited with PHP


I have a PHP website on a Linux server. I made a button next to the phone numbers on the site that writes a text file on the server with that number. The following code works.

$file = './gebruikers/'.$naam.'/nummer.txt';
$write = $_POST['num'];
file_put_contents($file, $write);

Now I made a C# application with TAPI3 to call the number in that text file. I use a FileSystemWatcher (watcher) to check the folder where php saves the text file so it makes the call everytime the file gets updated.

The following code checks which user is selected so it watches the folder of that user for the text file.

    private void cbGebruikers_SelectedIndexChanged(object sender, EventArgs e)
    {    
        if(cbGebruikers.Text != "")
        {
            comboBox1.Enabled = true;
            button6.Enabled = true;
            lblGebruiker.Visible = false;
            lblTelefoon.Visible = true;
        }

        path = @"\\192.168.1.9\SB Alarm programma\web-sb\gebruikers\" + cbGebruikers.Text;
        watcher.Path = path;
        watcher.NotifyFilter = NotifyFilters.LastAccess;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;

        lbltest.Text = watcher.Path.ToString();
    }

When the text file changes the following code will execute.

    private void OnChanged(object sender, FileSystemEventArgs e)
    {
        try
        {
            watcher.EnableRaisingEvents = false;

            telnummer = File.ReadAllText(path + "/nummer.txt");


            nummer = "0" + telnummer;


            this.Invoke((MethodInvoker)delegate
            {
                txtNummer.Text = nummer;
                MakeCall(nummer);
            });
        }
        finally
        {
            watcher.EnableRaisingEvents = true;
        }
    }

This code works, if I change the text file in the folder on my PC or on another PC that has access to the folder the application makes the call. But if PHP changes the text file nothing happens, but the last modified date does update.

Someone got experiencewith this?


Solution

  • This looks like an issue with using FileSystemWatcher in a cross platform architecture. FileSystemWatcher works by opening up a connection to the remote server, whose responsibility is responding when changes happen to the specified file. Windows platforms use the Win32 ReadDirectoryChanges(), while Linux boxes use Inotify API. Because there is no interface between the two APIs, the Linux box has no way of responding to the FileSystemWatcher.

    Relevant links http://msdn.microsoft.com/en-us/library/aa365465.aspx http://www.mono-project.com/docs/faq/technical/