GetDirectories() and GetFiles() returns no results

Is there a way for a Unity application to get full access to the filesystem? In my case, I am simply trying to allow the user to browse their file system to get to, for example, a profile photo, so they can give me the path and I can then load that file using WWW. However, any time I try to use System.IO.DirectoryInfo → GetDirectories(), the list of directories is empty. When I run in the Editor, I can see the current Unity project folder, but when I build, I just get an empty directory. For example

System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
);

var files = dirInfo.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly);

var dirs = dirInfo.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly);

Both dirs and files vars will 100% be empty (I have tried different wildcard options in there as well). Is there any reasonable way to do this?

From what I can see, it seems like I will need to either utilize some HTML/JS-based solution to get a input type=file, or, write native code for each individual supported platform. I am worried about going down either of these two routes though, because if I am still sandboxed from there, then I will waste a colossal amount of time.

Thanks!

This is completely target / platform / user dependent.

I’m also thinking you probably just want "*" for the wildcard.

2 Likes

I actually tried this with both * and ., to no avail. In my case, I am using Mac OSX when in editor mode and Mac as well as Android outside of editor mode. From what I can tell, the OS seems to only grant a Unity application access to the folders that were created by the application. I spoke to a library author who created an FS plugin for unity, and he explained that there is some notarization process needed for the application on Mac OSX. @Kurt-Dekker does “*” allow you to access the root level of the drive? I would be curious.

Of course not! MacOSX is an operating system, unlike Windows. :slight_smile:

You might be able to glob files on / but I doubt you could open anything for read, and it’s a cert you can’t write anything, I mean assuming you didn’t hack the system to just open it up completely, which nobody does.

2 Likes