0
votes

Edit: I just read that Microsoft products often delete and create a new file when saving, so we may need a completely different solution than below...

I'm writing some code to track file and folder changes on both Windows and Mac while my app is running, but it also needs to detect changes if it's not running.

I'm able to do this on Windows by obtaining folder and file ID's using the following post:

Getting a Folder ID C#

Now I need a solution for Mac, but after searching I've come up a bit empty. I can't find anything regarding a system-wide folder/file ID. The closest I came to a solution was using some sort of bookmark that could track the file as described below, but I haven't started yet:

https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW10

Seems I would have to store these bookmarks as Alias files to recall when the program starts back up.

Are there any other options for getting a global file/folder ID on Mac using C#?

1
unix filesystems use an inode to uniquely track files - not sure if there is a Mac API for that, but that is what I would search forJason

1 Answers

0
votes

You can use NSFileManager.GetAttributes to obtain an "inode" of the file (as Jason's comment calls out)

Note: Of course inodes change if moved across volumes, if a program that edits a file performs a delete and recreate vs. a replace, etc...

Example:

var ns = NSFileManager.DefaultManager;
var attribs = ns.GetAttributes("/Users/sushi/Desktop/foo.txt");
Console.WriteLine(attribs.SystemFileNumber);
File.Move("/Users/sushi/Desktop/foo.txt", "/Users/sushi/Desktop/foo2.txt");
var attribs2 = ns.GetAttributes("/Users/sushi/Desktop/foo2.txt");
Console.WriteLine(attribs2.SystemFileNumber);