11

Currently, I do this:

Names -> "name1|name2|name3"

Resource.Names.Split('|');

Is it possible to define those names as an array in the resource file so that I don't have to parse by splitting every time?

Something like the following perhaps?

Names[] -> "name1"
Names[] -> "name2"
Names[] -> "name3"
Resource.Names; // is of type string[]

Is there perhaps another, better way?

M4N
  • 94,805
  • 45
  • 217
  • 260
randomguy
  • 12,042
  • 16
  • 71
  • 101

2 Answers2

0

According to MSDN ResXResourceWriter page one can generate such resources using code provided there. Both string and array are serializable types, but of course resources contain Base64-encoded serialized array representation, which is not readable or writeable easily. But it is possible, and the type is detected correctly when adding generated resx file. I've used the following code to test:

using System.Resources;

internal class Program
{
    private static void Main(string[] args)
    {
        var arr = new[] { "Test", "string", "array", "resources" };

        using (var writer = new ResXResourceWriter("./output.resx"))
        {
            writer.AddResource("strings", arr);
            writer.Generate();
        }
    }
}

That said, no, it is not possible to have string array defined inside resource file in human-readable form without additional customizations.

n0ne
  • 103
  • 4
  • 17
-1

You can use the app settings to store your arrays. Go to your apps properties then the settings tab. You can set the name of your setting, select what type you want, the scope and the values.

For more info: http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx

Bruce Adams
  • 12,040
  • 6
  • 28
  • 37
  • Not really an option if the goal is to load a different array for each culture, though. – NightOwl888 Nov 10 '17 at 10:36
  • 1
    @NightOwl888 Array with localization is possible: https://social.msdn.microsoft.com/Forums/vstudio/en-US/3bd32763-e1f2-4266-ab1d-fd5655c11de4/localization-combobox-items?forum=wpf – OneWorld Feb 26 '20 at 13:54