Reading files in C # Mono Debian not working

I am using MonoDevelop on Debian, I have a problem with the following piece of code:

using (StreamReader sr = new StreamReader(File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))

      

It works great on Windows and works great when using MonoDevelop. But if I run it as mono output.exe

or sudo mono output.exe

method the first time with this code, it works fine. But the second time he fails. It doesn't write anything and no exception is thrown. I also tried replacing using

with try catch finally

, but no exception was thrown. I think it just cannot read the file.

The method with this code is called by the FileSystemWatcher change event. And I change the file via VIM.

Any ideas how I can find out where the problem is? Why is it working fine in MonoDevelop (so I can't trace it) and if it is run from the terminal it only works the first time (the method is always called, but the file is not readable and no exception is thrown even if it does not go inside. using {}).

Thank!

+3


source to share


1 answer


In section 3.2.8 I am facing another issue related to file watcher related events not firing (known issue has been fixed). Using 3.12.1 or 4.0.x +, the following code works fine for me, but this is not the case for R-PI. Take a snapshot of your RPI-Debian / Mono 3.2.8 installation to verify if this is an R-PI related bug / issue.

Sample output (Ctrl-C to exit):

>>mcs Program.cs
>>mono Program.exe 
Created watcher for /var/folders/hc/xf7j8x7j72dg7g098mwkhbs40000gp/T/tmp438e6660.tmp
Changed: '/var/folders/hc/xf7j8x7j72dg7g098mwkhbs40000gp/T/tmp438e6660.tmp', type: Changed
0Changed: '/var/folders/hc/xf7j8x7j72dg7g098mwkhbs40000gp/T/tmp438e6660.tmp', type: Changed
01Changed: '/var/folders/hc/xf7j8x7j72dg7g098mwkhbs40000gp/T/tmp438e6660.tmp', type: Changed
012Changed: '/var/folders/hc/xf7j8x7j72dg7g098mwkhbs40000gp/T/tmp438e6660.tmp', type: Changed
0123Changed: '/var/folders/hc/xf7j8x7j72dg7g098mwkhbs40000gp/T/tmp438e6660.tmp', type: Changed

      



Example Program.cs

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace FileWatcher
{
    class Program
    {
        static int counter = 0;

        public static void Main (string[] args)
        {
            var tempDir = Path.GetTempPath ();
            var tempFile = Path.GetTempFileName ();
            var fsw = new FileSystemWatcher (tempDir, Path.GetFileName (tempFile));
            fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
            fsw.Changed += WatcherOnChanged;
            fsw.EnableRaisingEvents = true;
            Console.WriteLine ("Created watcher for {0}", tempFile);
            var cts = new CancellationTokenSource ();
            Console.CancelKeyPress += (s, e) => {
                e.Cancel = true;
                cts.Cancel ();
            };    
            MainAsync (tempFile, cts.Token).Wait ();
            fsw.Dispose ();
            File.Delete (tempFile);
        }

        static async Task MainAsync (string fileName, CancellationToken token)
        {
            while (!token.IsCancellationRequested) {
                WriteFile (fileName);
                Thread.Sleep (2000);
            }
        }

        private static void WatcherOnChanged (object sender, FileSystemEventArgs eventArgs)
        {
            Console.WriteLine ("Changed: '{0}', type: {1}", eventArgs.FullPath, eventArgs.ChangeType);
            ReadFile (eventArgs.FullPath);
        }

        private static void ReadFile (string fileName)
        {
            using (var sr = new StreamReader (File.Open (fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) {
                Console.Write (sr.ReadLine ());
            }
        }

        private static void WriteFile (string fileName)
        {
            using (var sr = new StreamWriter (fileName, true)) {
                sr.Write (counter++);
            }
        }
    }
}

      

0


source







All Articles