6

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.

Kaze
  • 101
  • 1
  • 6
  • 1
    Writing file metadata is ridiculously painful, you have to support all the bugs in every program that reads it back again. There are a lot of those bugs, standardization is poor or grossly outdated. Windows doesn't attempt the feat, you have to use a library. – Hans Passant Jun 04 '14 at 16:09
  • I understand what you mean, but editing an existing property should be easy. I think I finally found a way using the WinAPI. I'll post an answer later if it's conclusive. – Kaze Jun 04 '14 at 19:30
  • [Windows Property System}(https://msdn.microsoft.com/en-us/library/windows/desktop/ff728898(v=vs.85).aspx) is the top level of a series of articles in Windows Dev Center. The article [Property System Overview](https://msdn.microsoft.com/en-us/library/windows/desktop/ff728871(v=vs.85).aspx) describes the Windows Property system. It looks like it is not well supported in Windows XP however has been improved beginning with Windows Vista. It appears to use a COM interface. Probably easier to do in Managed code than using COM directly. – Richard Chambers Aug 05 '16 at 02:38

2 Answers2

4

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.

Kaze
  • 101
  • 1
  • 6
3

You can write to the ShellFile directly by setting the value of the properties without ShellPropertyWriter:

var shellFile = ShellFile.FromFilePath(filePath);

shellFile.Properties.System.Author.Value = new string[] { "MyTest", "Test" };
shellFile.Properties.System.Music.Artist.Value = new string[] { "MyTest", "Test" };
shellFile.Properties.System.Music.DisplayArtist.Value = "Test";

Just be aware, that to be able to edit codec-specific fields of a file, it's necessary to have the codec installed on the computer.

M. Schena
  • 2,039
  • 1
  • 21
  • 29