-
Notifications
You must be signed in to change notification settings - Fork 26
Advanced Attribute Access
All nodes in VirtualFileSystem provide attributes accessible via the INode.Attributes property. The basic attributes are:
- ReadOnly
- IsHidden
- CreationTime
- LastAccessTime
- LastWriteTime
- Exists
These attributes can be retrived and set statically using similarly named properties. These properties can also be accessed dynamically using the string name of the property and the INode.this[] accessor.
IFile file = FileSystemManager.Default.ResolveFile("temp://test.txt");
Console.WriteLine(file.Attributes.LastWriteTime);
Console.WriteLine(file.Attributes["LastWriteTime"]);
You can convert attribute values to a specific type or specific resolution.
IFile file = FileSystemManager.Default.ResolveFile("temp://test.txt");
// Rounds the LastWriteTime to the nearest second
Console.WriteLine(file.Attributes["LastWriteTime|secondsresolution"]);
// Converts length to a string
var s = (string)file.Attributes["Length|string"];
This method of conversion is useful when attributes need to be provided as arguments. For example, the DirectoryHashingServiceType allows to you specify the attributes you would like hashed as a string list of attribute names. By specifying "LastWriteTime|secondsresolution" you can make sure that the write time of files will only be hashed down to seconds. This is useful when comparing directory hashes between file systems that store datetimes down to the millisecond (e.g. NTFS) and file systems that only store datetimes down to the second (e.g. ext2fs).
Most operation systems support storing extended (custom) attributes on files. Platform.VirtualFileSystem supports extended attributes directly on Unix-like operating systems and stores extended attributes on Windows/NTFS using alternate data streams. You can read and write to extended attributes by prefix the attribute name with "extended:".
Attribute values need to be a byte array (byte[]) otherwise the the ToString() value will be converted to a UTF-8 byte array. Getting a value of an extended attribute will always return a byte array unless the attribute is converted inline.
IFile file = FileSystemManager.Default.ResolveFile("temp://test.txt");
file.Attributes["extended:customattribute"] = "Hello";
var byteArrayValue = (byte[])file.Attributes["extended:customattribute"];
var stringValue = (string)file.Attributes["extended:customattribute|string"];