/// <summary>Gets all of the attributes on a file.</summary> /// <returns>A collection of the attributes from the file.</returns> public override System.Collections.IDictionary GetAttributes() { if (_editor == null) { throw new ObjectDisposedException(GetType().Name); } Hashtable propsRetrieved = new Hashtable(); // Get the number of attributes ushort attributeCount = _editor.GetAttributeCount(0); // Get each attribute by index for (ushort i = 0; i < attributeCount; i++) { MetadataItemType attributeType; StringBuilder attributeName = null; byte[] attributeValue = null; ushort attributeNameLength = 0; ushort attributeValueLength = 0; // Get the lengths of the name and the value, then use them to create buffers to receive them uint reserved = 0; _editor.GetAttributeByIndex(i, ref reserved, attributeName, ref attributeNameLength, out attributeType, attributeValue, ref attributeValueLength); attributeName = new StringBuilder(attributeNameLength); attributeValue = new byte[attributeValueLength]; // Get the name and value _editor.GetAttributeByIndex(i, ref reserved, attributeName, ref attributeNameLength, out attributeType, attributeValue, ref attributeValueLength); // If we got a name, parse the value and add the metadata item if (attributeName != null && attributeName.Length > 0) { object val = ParseAttributeValue(attributeType, attributeValue); string key = attributeName.ToString().TrimEnd('\0'); propsRetrieved[key] = new MetadataItem(key, val, attributeType); } } // Return the parsed items return(propsRetrieved); }
/// <summary>Converts a value to the target type and gets its byte representation.</summary> /// <param name="item">The item whose value is to be translated.</param> /// <param name="valueData">The output byte array.</param> protected static bool TranslateAttributeToByteArray(MetadataItem item, out byte [] valueData) { int valueLength; switch (item.Type) { case MetadataItemType.Dword: valueData = BitConverter.GetBytes((int)item.Value); return(true); case MetadataItemType.Word: valueData = BitConverter.GetBytes((short)item.Value); return(true); case MetadataItemType.Qword: valueData = BitConverter.GetBytes((long)item.Value); return(true); case MetadataItemType.Boolean: valueData = BitConverter.GetBytes(((bool)item.Value) ? 1 : 0); return(true); case MetadataItemType.String: string strValue = item.Value.ToString(); valueLength = (strValue.Length + 1) * 2; // plus 1 for null-term, times 2 for unicode valueData = new byte[valueLength]; Buffer.BlockCopy(strValue.ToCharArray(), 0, valueData, 0, strValue.Length * 2); valueData[valueLength - 2] = 0; valueData[valueLength - 1] = 0; return(true); default: valueData = null; return(false); } }
/// <summary>Gets all of the attributes on a file.</summary> /// <returns>A collection of the attributes from the file.</returns> public override System.Collections.IDictionary GetAttributes() { if (_editor == null) throw new ObjectDisposedException(GetType().Name); Hashtable propsRetrieved = new Hashtable(); // Get the number of attributes ushort attributeCount = _editor.GetAttributeCount(0); // Get each attribute by index for(ushort i = 0; i < attributeCount; i++) { MetadataItemType attributeType; StringBuilder attributeName = null; byte[] attributeValue = null; ushort attributeNameLength = 0; ushort attributeValueLength = 0; // Get the lengths of the name and the value, then use them to create buffers to receive them uint reserved = 0; _editor.GetAttributeByIndex(i, ref reserved, attributeName, ref attributeNameLength, out attributeType, attributeValue, ref attributeValueLength); attributeName = new StringBuilder(attributeNameLength); attributeValue = new byte[attributeValueLength]; // Get the name and value _editor.GetAttributeByIndex(i, ref reserved, attributeName, ref attributeNameLength, out attributeType, attributeValue, ref attributeValueLength); // If we got a name, parse the value and add the metadata item if (attributeName != null && attributeName.Length > 0) { object val = ParseAttributeValue(attributeType, attributeValue); string key = attributeName.ToString().TrimEnd('\0'); propsRetrieved[key] = new MetadataItem(key, val, attributeType); } } // Return the parsed items return propsRetrieved; }
/// <summary>Sets the value of a string attribute.</summary> /// <param name="items">The metadata items collection.</param> /// <param name="name">The name of the attribute.</param> /// <param name="value">The new value of the attribute.</param> public static void SetMetadataItemAsString(IDictionary items, string name, string value) { items[name] = new MetadataItem(name, value, MetadataItemType.String); }
/// <summary>Converts a value to the target type and gets its byte representation.</summary> /// <param name="item">The item whose value is to be translated.</param> /// <param name="valueData">The output byte array.</param> protected static bool TranslateAttributeToByteArray(MetadataItem item, out byte [] valueData) { int valueLength; switch(item.Type) { case MetadataItemType.Dword: valueData = BitConverter.GetBytes((int)item.Value); return true; case MetadataItemType.Word: valueData = BitConverter.GetBytes((short)item.Value); return true; case MetadataItemType.Qword: valueData = BitConverter.GetBytes((long)item.Value); return true; case MetadataItemType.Boolean: valueData = BitConverter.GetBytes(((bool)item.Value) ? 1 : 0 ); return true; case MetadataItemType.String: string strValue = item.Value.ToString(); valueLength = (strValue.Length + 1) * 2; // plus 1 for null-term, times 2 for unicode valueData = new byte[valueLength]; Buffer.BlockCopy(strValue.ToCharArray(), 0, valueData, 0, strValue.Length * 2); valueData[valueLength - 2] = 0; valueData[valueLength - 1] = 0; return true; default: valueData = null; return false; } }
/// <summary>Sets the value of a string attribute.</summary> /// <param name="items">The metadata items collection.</param> /// <param name="name">The name of the attribute.</param> /// <param name="value">The new value of the attribute.</param> public static void SetMetadataItemAsString(IDictionary items, string name, string value) { items[name] = new MetadataItem(name, value, MetadataItemType.String); }