Search code examples
c#windows-api-code-packfile-propertieswindows-property-system

Is it possible to set/edit a file extended properties with Windows API Code Pack?


I'd like to know if it's possible to set/edit a file extended properties (Explorer: Right-click > Properties > Details) using the Windows API Code Pack.

var shellFile = Microsoft.WindowsAPICodePack.Shell.ShellObject.FromParsingName(filePath);
var artistName = shellFile.Properties.GetProperty(SystemProperties.System.Music.DisplayArtist).ValueAsObject.ToString();
var duration = TimeSpan.FromMilliseconds(Convert.ToDouble(shellFile.Properties.GetProperty(SystemProperties.System.Media.Duration).ValueAsObject) * 0.0001);

I use these few lines to get the properties I want, but I don't know how to edit one of them (the artist name for example). I know I can use taglib-sharp, but I'll use it only if there is no solution without external code.

Thanks you all for taking the time to help me.


Solution

  • I found a way to edit some properties with ShellPropertyWriter but some properties are read-only.

    var shellFile = ShellFile.FromParsingName(filePath);
    ShellPropertyWriter w = shellFile.Properties.GetPropertyWriter();
    try
    {
        w.WriteProperty(SystemProperties.System.Author, new string[] { "MyTest", "Test" });
        w.WriteProperty(SystemProperties.System.Music.Artist, new string[] { "MyTest", "Test" });
        w.WriteProperty(SystemProperties.System.Music.DisplayArtist, "Test");
    }
    catch (Exception ex)
    {
    
    }
    w.Close();
    

    In this sample, the 2 first occurences of ShellPropertyWriter.WriteProperty() will do exactly the same, edit the "Contributing artists" field of the file (Explorer: Right-click > Properties > Details). The third call will throw an "Access denied" exception. Some are editable, others are not. Just need to try.