0

So I am trying to create a mass media editor which allows the user to update the properties of media file types. I am using the ShellFile API pack from Microsoft.

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;

So I am able to use the ShellFile class to edit metadata properties such as Rating, Tags, Title, Application Name, etc.

I am running into an issue with fields that end up having multiple items (such as Tags, Authors, Artists) because they are of the type:

public ShellProperty<string[]> Author { get; }

So for example say that you have multiple items.

Properties of a Picture (Focus on Author)

If you try to update these outside of the properties window through code, say something like:

       ShellFile shellFile = ShellFile.FromFilePath(((Media)mediaFile).FilePath);

        if (mediaFile is Picture)
        { 
            String[] authorArray = ((Picture)mediaFile).Authors.Split(',');

            shellFile.Properties.System.Author.Value = authorArray;
            /*** Writing other values to mediaFile ***/
        }

This leads into you getting this error if you try to add more authors/tags/whatever to a file:

System.ArgumentOutOfRangeException: 'A value had to be truncated in a string or rounded if a numeric value. Set AllowTruncatedValue to true to prevent this exception. Parameter name: value'

I'm wondering if there is a way to get around this? Since it's a property of the API and Arrays are immutable & I can't actually make it larger, or use something more friendly like a List <String>. I tried Array.Resize however I didn't have any luck with it.

rmcombs
  • 43
  • 5
  • 1
    What is the exact content of authorArray when you try to assign it? Assigning some valid string array seems to be the way to go (https://stackoverflow.com/a/37987288/5265292), so if your file as well as the author array is well formatted, this looks like it should work. – grek40 Feb 25 '19 at 19:36
  • Below I've added a screenshot of what is inside of authorArray, as well as the ShellFile's author attribute. ![authorArray](https://imgur.com/38o4tC4). ![ShellFile.Author.Value](https://imgur.com/NUGh5kR). – rmcombs Feb 25 '19 at 20:22

1 Answers1

1

So I found the answer as to why I was getting the Out of Range exception. While trying to find a way to create a new string[] {} format with my updated values in grek40's post I came across the following codesnippet which worked regardless of the size of the old property values:

string[] x =  {"1","2","3"};
x = x.Select(( Y ) => { return Y; }).ToArray();

I also noticed that the formatting when it was loaded up looked as such:

1,2,3

It seems that when I try to create an authorArray I use the following code:

string[] authorArray = ((Picture)mediaFile).Authors.Split(',');

if you have a given string such as

Author 1, Author 2, Author 3

this splits the array into the values of

  • "Author 1"
  • " Author 2"
  • " Author 3"

Apparently the whitespace on the beginning of a entry seems to cause a truncation to happen. Basically I had to sanitize the outputs of the string array with .Trim() before they were assigned to the ShellFile property.

rmcombs
  • 43
  • 5