NSUrl CreateApplicationSupportDirectory()
        {
            NSFileManager fileManager = NSFileManager.DefaultManager;

            NSUrl[] URLs = fileManager.GetUrls(NSSearchPathDirectory.ApplicationSupportDirectory, NSSearchPathDomain.User);
            NSUrl   url  = URLs [URLs.Length - 1];

            url = url.Append(ApplicationDocumentsDirectoryName, false);
            NSError error;

            NSDictionary properties = url.GetResourceValues(new NSString[] { NSUrl.IsDirectoryKey }, out error);

            if (properties != null)
            {
                var isDirectoryNumber = (NSNumber)properties [NSUrl.IsDirectoryKey];
                if (isDirectoryNumber != null && !isDirectoryNumber.BoolValue)
                {
                    Console.WriteLine("Could not access the application data folder.");
                    return(null);
                }
            }
            else if (error.Code == (int)NSCocoaError.FileReadNoSuchFile)
            {
                bool ok = fileManager.CreateDirectory(url.Path, true, null);
                if (!ok)
                {
                    Console.WriteLine("Error occured: {0}", error.LocalizedDescription);
                    return(null);
                }
            }

            return(url);
        }
示例#2
0
        public static bool IsFolder(this NSUrl url)
        {
            var resources = url.GetResourceValues(new[]
            {
                NSUrl.IsDirectoryKey, NSUrl.IsPackageKey
            }, out var error);

            if (error != null)
            {
                Debug.WriteLine(error);
                return(false);
            }

            var isDir     = resources.TryGetValue(NSUrl.IsDirectoryKey, out var isDirObj) && ((NSNumber)isDirObj).BoolValue;
            var isPackage = resources.TryGetValue(NSUrl.IsPackageKey, out var isPackageObj) && ((NSNumber)isPackageObj).BoolValue;

            return(isDir && !isPackage);
        }
示例#3
0
        public void IsExcludedFromBackupKey()
        {
            //TestRuntime.AssertSystemVersion (PlatformName.MacOSX, 10, 8, throwIfOtherPlatform: false);
            TestRuntime.AssertSystemVersion(PlatformName.MacOSX, 10, 9, throwIfOtherPlatform: false);              // 10.8 fails DoNotBackupMe-1
            TestRuntime.AssertSystemVersion(PlatformName.MacOSX, 10, 10, throwIfOtherPlatform: false);             // 10.10 fails DoNotBackupMe-1

            // NOTE: this test was failing with either NullReferenceException or InvalidCastException
            // when we used CFBoolean as a NSObject (i.e. CFBoolean.TrueObject). The test order execution
            // was important to track this down

            NSObject value;

            Assert.True(NSBundle.MainBundle.ExecutableUrl.TryGetResource(NSUrl.IsExcludedFromBackupKey, out value), "MainBundle");
            Assert.That(value, Is.TypeOf(typeof(NSNumber)), "NSNumber");
            Assert.That((int)(value as NSNumber), Is.EqualTo(0), "0");

            string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), $"DoNotBackupMe-NSUrl-{Process.GetCurrentProcess ().Id}");

            try {
                File.WriteAllText(filename, "not worth a bit");
                using (NSUrl url = NSUrl.FromFilename(filename)) {
                    Assert.True(url.TryGetResource(NSUrl.IsExcludedFromBackupKey, out value));
                    Assert.That((int)(value as NSNumber), Is.EqualTo(0), "DoNotBackupMe-0");

                    url.SetResource(NSUrl.IsExcludedFromBackupKey, (NSNumber)1);

                    Assert.True(url.TryGetResource(NSUrl.IsExcludedFromBackupKey, out value));
                    Assert.That((int)(value as NSNumber), Is.EqualTo(1), "DoNotBackupMe-1");

                    NSError      error;
                    NSDictionary dict = url.GetResourceValues(new NSString [] { NSUrl.IsExcludedFromBackupKey }, out error);
                    Assert.Null(error, "error");
                    Assert.That(dict.Keys [0], Is.EqualTo(NSUrl.IsExcludedFromBackupKey), "Key");
                    Assert.That((int)(dict.Values [0] as NSNumber), Is.EqualTo(1), "Value");
                }
            }
            finally {
                // otherwise the attribute won't reset even if the file is overwritten
                File.Delete(filename);
            }
        }