Bug 11771 - IsolatedStorageFile.GetFileNames() throws exception w/ nested dirs
Summary: IsolatedStorageFile.GetFileNames() throws exception w/ nested dirs
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-04-13 17:05 UTC by Jonathan Pryor
Modified: 2017-06-01 02:02 UTC (History)
4 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Jonathan Pryor 2013-04-13 17:05:40 UTC
Context: http://forums.xamarin.com/discussion/3092/system-security-securityexception

(Yes, a Xamarin.Android bug, but it applies to desktop mono.)

Consider the following app:

  // IsolatedStorage DeleteDirectory
  using System;
  using System.IO;
  using System.IO.IsolatedStorage;
  class Test {
    public static void Main ()
      string dir = Path.Combine("Dir", "Level");
      using (var f = IsolatedStorageFile.GetUserStoreForAssembly ())
        f.CreateDirectory (dir);
        using (var o = f.CreateFile(Path.Combine(dir, "o2")))
          o.Write(new byte[] { 1, 2, 3 }, 0, 3);
      DeleteDirectory (dir);
    public static void DeleteDirectory(string directory)
      using (var store = IsolatedStorageFile.GetUserStoreForAssembly ())
        if (store.DirectoryExists(directory))
          // System.Security.SecurityException HERE!
          var files = store.GetFileNames(Path.Combine(directory, "*"));
          foreach (string file in files)
            store.DeleteFile(Path.Combine(directory, file));

Run it, and it dies with a SecurityException:

> $ dmcs -debug+ iso-del.cs && mono --debug iso-del.exe 
> Unhandled Exception:
> System.Security.SecurityException: A security error has been detected.
>   at System.IO.IsolatedStorage.IsolatedStorageFile.GetFileNames (System.String searchPattern) [0x000ba] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-3.0.7/_build/mono-3.0.7.git/mcs/class/corlib/System.IO.IsolatedStorage/IsolatedStorageFile.cs:850 
>   at (wrapper remoting-invoke-with-check) System.IO.IsolatedStorage.IsolatedStorageFile:GetFileNames (string)
>   at Test.DeleteDirectory (System.String directory) [0x00015] in /Users/jon/tmp/csharp/iso-del.cs:27 
>   at Test.Main () [0x0006f] in /Users/jon/tmp/csharp/iso-del.cs:17 

Here's the "funny" thing; it's caused by nested directories. Change line 9 to:

      string dir = "Dir";

and it works without exception.

(Note: to "work without exception" it needs to be a _clean_ isolated storage environment. Thus, if you run the "buggy" version you'll need to nuke ~/.config/.isolated-storage/*; after nuking, the "fixed" app will work w/o error.)

No SecurityException should be generated for nested directories.
Comment 1 Jordan Earls 2013-08-23 10:46:33 UTC
This bug is actually not a bug depending on how you look at it. This is the commit responsible for it: https://github.com/mono/mono/commit/b79a9d584792d4b74c8269acecab6d7eef526015

It would appear that the Mono team was following Windows Phone 7 development at the time and when it came out, it had this huge breaking change/bug in it's IsolatedStorage implementation(from what I can tell in my own legacy code to workaround it). So, Mono decided to follow and implement all APIs to the do the same as Windows Phone. This was fixed in subsequent versions of Windows Phone and Silverlight. This code appears to have almost sneaked in even. There was no bug report or anything for this "fix" in Mono. I have a feeling that if there were, it would've been spotted and thrown out as "we don't want to copy Microsoft's bug".. So, I suggest reverting the relevant portion of that commit
Comment 2 Kent Green [MSFT] 2015-02-26 14:26:31 UTC
Customer from this desk case https://xamarin.desk.com/agent/case/127553, reports hitting this issue. 

The exception is thrown if they call the IsolatedStorageFile.GetFileNames with nested directories. It works only on first level directory. As it stands they can't delete any files created in nested directories without knowing the exact names.

They also asked what the status of this bug is & if there are any known workarounds.
Comment 3 Melbourne Developer 2017-03-20 05:12:13 UTC
I'm getting this problem as well!

I'm getting it on Android. I am trying to iterate through files in a nested folder. But I get the error "A security error has been detected"

Please fix this bug!
Comment 4 Melbourne Developer 2017-03-21 05:01:56 UTC
When will this be fixed?
Comment 5 Melbourne Developer 2017-04-05 00:19:56 UTC
Please fix this bug.
Comment 6 Melbourne Developer 2017-06-01 02:02:47 UTC
Has this been fixed?

Note You need to log in before you can comment on or make changes to this bug.