是否可以使用Windows API代码包设置/编辑文件扩展属性?

时间:2014-06-04 14:30:03

标签: c# windows-api-code-pack file-properties

我想知道是否可以使用Windows API代码包设置/编辑文件扩展属性(资源管理器:右键单击>属性>详细信息)。

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);

我使用这几行来获取我想要的属性,但我不知道如何编辑其中一个(例如艺术家名称)。 我知道我可以使用taglib-sharp,但只有在没有外部代码的解决方案时我才会使用它。

感谢大家抽出时间帮助我。

2 个答案:

答案 0 :(得分:4)

我找到了一种用ShellPropertyWriter编辑某些属性的方法,但有些属性是只读的。

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();

在此示例中,ShellPropertyWriter.WriteProperty()的2个首次出现将完全相同,编辑"贡献艺术家"文件的字段(资源管理器:右键单击>属性>详细信息)。第三个电话会抛出一个" Access denied"例外。 有些是可编辑的,有些则不是。只需要尝试。

答案 1 :(得分:1)

您可以通过设置没有ShellFile的属性的值来直接写入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";

请注意,要能够编辑文件的编解码器特定字段,必须在计算机上安装编解码器。