• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by diginikkari · May 03, 2015 at 09:40 PM · unity 5macfilesystem

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());
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bcao · May 26, 2015 at 09:34 AM 0
Share

Im having the same issue as you, what version of unity are you on?

avatar image diginikkari · May 26, 2015 at 11:56 AM 0
Share

I'm having the latest version: 5.0.1f1

avatar image greggman · Jan 24, 2016 at 06:37 PM 1
Share

$$anonymous$$e too. Sigh

avatar image jojizaidi · Jul 15, 2016 at 12:53 PM 0
Share

Did anyone get this to work?

avatar image wirmachenbunt · Mar 05, 2019 at 10:53 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dbelay · Jul 25, 2016 at 03:40 PM 0
Share

Thanks, it works!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

can't install file dmg on Mac 0 Answers

[macOS] cant build windows version,[macOS] Cant build windows version 0 Answers

Unity x Nintendo Switch - How to save file? 0 Answers

How does one build an app with iCloud/CloudKit with Unity? 0 Answers

Unable to build project for Android with Mac Unity version 2017.4.1f1 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges