- Home /
How to monitor file system in MAC
Hi,
has anyone found solution for using filesystemwatcher or something similar in MAC? I try to monitor if USB-stick has been inserted.
FileSystemWatcher is working in linux and windows, but not in MAC. Events are not raised.
using System;
using UnityEngine;
using System.Collections;
using System.IO;
using System.Security.Permissions;
public class Watcher : MonoBehaviour {
public string fileToWatch = "*.*";
public string Path = "/Volumes";
private FileSystemWatcher watcher;
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
void Start ()
{
watcher = new FileSystemWatcher ();
watcher.Path = Path;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = fileToWatch;
watcher.Renamed += new RenamedEventHandler (OnRenamed);
watcher.Changed += new FileSystemEventHandler (OnChanged);
watcher.Created += new FileSystemEventHandler (OnChanged);
watcher.Deleted += new FileSystemEventHandler (OnChanged);
watcher.Error += new ErrorEventHandler(OnError);
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
}
public void OnChanged (object source, FileSystemEventArgs e)
{
WatcherChangeTypes wct = e.ChangeType;
Debug.Log (e.FullPath + ": " + wct.ToString ());
}
public void OnRenamed (object source, RenamedEventArgs e)
{
WatcherChangeTypes wct = e.ChangeType;
Debug.Log ("file " + e.OldFullPath + " renamed to " + e.FullPath + ": " + wct.ToString ());
}
public void OnError(object source, ErrorEventArgs e)
{
Debug.Log("Error detected: " + e.GetException().GetType().ToString());
}
}
Im having the same issue as you, what version of unity are you on?
hey, i'm trying to add [PermissionSet(SecurityAction.Demand, Name="FullTrust")] to my code but visual studio complains "permissionsetattribute not found".
i have all your usings but still no luck. any idea what might be missing ?
cheers
edit: seems solved, change the .net version to 4.0 in unity
Answer by jojizaidi · Jul 18, 2016 at 12:13 PM
I got this to work. You need to call this before you set your watcher or even once. It sets an environment variable.
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
Environment.SetEnvironmentVariable("MONO_MANAGED_WATCHER", "enabled");
#endif
Your answer
Follow this Question
Related Questions
Can't run unity 2.6.1 on mac os x 10.6.3 2 Answers
Building an IOS app with Facebook SDK integration 2 Answers
Could not connect to debugger 0 Answers
What is the version of unity 5 that works in High Sierra 0 Answers
Better way to deploy on Mac? 0 Answers