在C#Mono Debian中读取文件不起作用

本文关键字:文件 不起作用 读取 C#Mono Debian | 更新日期: 2023-09-27 18:01:05

我在Debian上使用MonoDevelop,我对以下代码有问题:

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

它在Windows中运行良好,通过MonoDevelop运行时运行良好。但是,如果我像mono output.exe或sudo mono output.exe一样运行它,那么第一次调用带有此代码的方法就可以了。但第二次失败了。它不写任何东西,不会抛出异常。我也尝试过用try catch finally替换using,但没有抛出异常。我认为它就是无法读取文件。

具有此代码的方法由FileSystemWatcher更改事件调用。我正在通过VIM更改文件。

有什么想法吗?我该如何找出问题所在?为什么在MonoDevelop中它工作得很好(所以我无法跟踪它(,如果从终端运行,它只是第一次工作(方法总是被调用,但文件没有被读取,也没有抛出异常,它甚至不会使用{}进入内部(。

谢谢!

在C#Mono Debian中读取文件不起作用

在3.2.8下,我遇到了一个与文件观察程序事件未触发有关的不同问题(已知问题已修复(。使用3.12.1或4.0.x+,以下代码对我来说很好,但这不适用于R-PI。在你的RPI Debian/Mono 3.2.8安装中尝试一下,看看它是否是一个与R-PI相关的错误/问题。

示例输出(按Ctrl-C退出(:

>>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

示例程序.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++);
            }
        }
    }
}