Forum Xamarin.iOS

How can I get « tags » of files/folder programmatically ?

NinineaNininea USUniversity ✭✭✭
edited July 2016 in Xamarin.iOS

Hi Guys,

Is there any possibility to get and set "Tag" property to file?

Tagged:

Posts

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Welcome to the Xamarin.Mac boards. Often you can find answers to your Xamarin.Mac questions by google searching for "Cocoa X", where X is the specific thing you are curious about. "cocoa get file tag" brought up a few StackOverflow posts.

    95% of those answers will be in objective-c, however I wrote some documentation to help you convert those to C#:

    https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/
    https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/

    Here's a specific answer to your question to show you how the conversion looks in the end.

            // http://stackoverflow.com/a/23227906/36782
            NSUrl url = NSUrl.FromFilename (@"/Users/donblas/tmp/1");
            NSObject o;
            url.TryGetResource (NSUrl.LabelNumberKey, out o);
            NSNumber value = (NSNumber)o;
            switch (value.Int32Value)
            {
                case 0:
                    System.Console.WriteLine ("None");
                    break;
                case 1:
                    System.Console.WriteLine ("Gray");
                    break;
                case 2:
                    System.Console.WriteLine ("Green");
                    break;
                case 3:
                    System.Console.WriteLine ("Purple");
                    break;
                case 4:
                    System.Console.WriteLine ("Blue");
                    break;
                case 5:
                    System.Console.WriteLine ("Yellow");
                    break;
                case 6:
                    System.Console.WriteLine ("Red");
                    break;
                case 7:
                    System.Console.WriteLine ("Orange");
                        break;
            }
    
  • NinineaNininea USUniversity ✭✭✭

    @ChrisHordern Thanks for answer ^_^

    I have found better solution for it : https://stackoverflow.com/questions/38633600/read-write-file-tag-in-cocoa-app-os-x-with-swift-or-obj-c

    here is our c# code:

    var url = new NSUrl(filePath, false);
                   var resource = new NSObject();
                   var isResource = url.TryGetResource(new NSString("NSURLTagNamesKey"), out resource);
                   if (isResource && resource != null)
                   {
                       var tagArray = (NSArray)resource;
                       for (uint i = 0; i < (uint)(tagArray).Count; i++)
                       {
                           var tag = tagArray.GetItem<NSObject>(new nuint(i)).ToString();
                           tags.Add(tag);
                       }
                   }
    
Sign In or Register to comment.