How do I add tags / keywords to a windows file pro

2019-07-17 00:23发布

问题:

Ideally I would like to use the shell class to add tags to my office documents but I think the tags property is a read only item this way. Does anyone have any other ways?

There is very little on the subject. Thank you for your help.

回答1:

I looked into the shellfile class a little more. The answer was staring me right in the face.

string[] keywords = new string[x];
var shellFile = ShellFile.FromFilePath(file);
shellFile.Properties.System.Keywords.Value = keywords;

to get the keywords already added to the file use:

var tags = (string[])shellFile.Properties.System.Keywords.ValueAsObject;
tags = tags ?? new string[0];

if (tags.Length != 0)
{
    foreach (string str in tags)
    {
        // code here
    }
}

and done!