c# - Xamarin/C# 中的 Android FileObserver 示例?

标签 c# android file xamarin observers

<分区>

我需要有关在 Xamarin c# (Android) 中创建文件观察器的指导

一些可行的例子会很棒!

我曾尝试将 Java 的转换为 C#,但由于我在 C# 环境中缺乏经验,编译时会抛出太多错误……事实证明,在 C# 和 Java 中获取编写引用代码令人恼火..

所以,拜托!,可能有人会给我指出一些可行的方法

这是一个文件观察器的java例子 https://gist.github.com/shirou/659180

最佳答案

创建一个继承自 Android.OS.FileObserver 的类, 你只需要实现 OnEvent()和一个(+)构造函数。看过一次之后,它是一个非常简单的模式...;-)

注意事项:

  • 路径上观看,如果您需要按文件过滤,请在 OnEvent 中执行
  • 不要让你的 FileObserver 对象得到 GC,否则你的 OnEvents 会神奇地停止 :-/
  • 记得调用 StartWatching() 以接收 OnEvent 调用

FileObserver 类:

using System;
using Android.OS;
using Android.Util;

namespace MyFileObserver
{
    public class MyPathObserver : Android.OS.FileObserver
    {
        static FileObserverEvents _Events = (FileObserverEvents.AllEvents);
        const string tag = "StackoverFlow";

        public MyPathObserver (String rootPath) : base(rootPath, _Events)
        {
            Log.Info(tag, String.Format("Watching : {0}", rootPath)); 
        }

        public MyPathObserver (String rootPath, FileObserverEvents events) : base(rootPath, events)
        {
            Log.Info(tag, String.Format("Watching : {0} : {1}", rootPath, events)); 
        }

        public override void OnEvent(FileObserverEvents e, String path)
        {
            Log.Info(tag, String.Format("{0}:{1}",path, e)); 
        }
    }
}

示例用法:

var pathToWatch = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
// Do not let myFileObserver get GC'd, stash it's ref in an activty, or ...
myFileObserver = new MyPathObserver (pathToWatch);
myFileObserver.StartWatching (); // and StopWatching () when you are done...
var document = Path.Combine(pathToWatch, "StackOverFlow.txt");
button.Click += delegate {
    if (File.Exists (document)) {
        button.Text = "Delete File";
        File.Delete (document);
    } else {
        button.Text = "Create File";
        File.WriteAllText (document, "Foobar");
    }
};

adb logcat 输出(点击测试按钮时):

I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
I/StackoverFlow( 3596): StackOverFlow.txt:Delete
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
I/StackoverFlow( 3596): StackOverFlow.txt:Delete
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite

关于c# - Xamarin/C# 中的 Android FileObserver 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703113/

相关文章:

c# - 如何将多字符串与 C# 字符串集合相互转换?

android - Eclipse ADT : Project name "appcompat_v7" being created after creating every new project

android - ActionbarSherlock 混淆了 Fragment 的 onAttach() 方法?

c - 如何使用C编程语言删除文本文件中的特定行

java - 使用Java遍历文件和目录

c# - ASP.net 中的浏览器滚动条(如何设置)

c# - Azure 表存储查询性能缓慢

android - 可以在 Android 应用程序中以编程方式打开 Spinner 吗?

c# - 为什么我需要一段一段地读取文件来缓冲?

C# - 数据库密集型应用程序的多核线程