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

C# 是否可以使用Windows API代码包设置/编辑文件扩展属性?,c#,windows-api-code-pack,file-properties,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).ValueAsOb

我想知道是否可以使用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,但只有在没有外部代码的解决方案时,我才会使用它


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

我找到了一种方法,可以使用
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()
将执行完全相同的操作,即编辑文件的“贡献艺术家”字段(资源管理器:右键单击>属性>详细信息)。第三个调用将抛出“拒绝访问”异常。
有些是可编辑的,有些不是。只需尝试。

您可以直接写入
外壳文件
,方法是在不使用
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";

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

编写文件元数据是一件非常痛苦的事情,您必须支持每个重新读取该文件的程序中的所有错误。有很多这样的错误,标准化很差或者非常过时。Windows没有尝试这一壮举,您必须使用库。我理解您的意思,但编辑现有属性应该很容易。我想我终于找到了使用WinAPI的方法。如果答案是肯定的,我会在稍后发布答案。介绍Windows属性系统。看起来它在Windows XP中没有得到很好的支持,但是从Windows Vista开始,它得到了改进。它似乎使用COM接口。在托管代码中可能比直接使用COM更容易。您是否知道是否可以添加这样的标记?