GetResourceSet Method

ResourceManager.GetResourceSet Method (CultureInfo, Boolean, Boolean)

 

Retrieves the resource set for a particular culture.

Namespace:   System.Resources
Assembly:  mscorlib (in mscorlib.dll)

public virtual ResourceSet GetResourceSet(
	CultureInfo culture,
	bool createIfNotExists,
	bool tryParents
)

Parameters

culture
Type: System.Globalization.CultureInfo

The culture whose resources are to be retrieved.

createIfNotExists
Type: System.Boolean

true to load the resource set, if it has not been loaded yet; otherwise, false.

tryParents
Type: System.Boolean

true to use resource fallback to load an appropriate resource if the resource set cannot be found; false to bypass the resource fallback process. (See the Remarks section.)

Return Value

Type: System.Resources.ResourceSet

The resource set for the specified culture.

Exception Condition
ArgumentNullException

The culture parameter is null.

MissingManifestResourceException

tryParents is true, no usable set of resources has been found, and there are no default culture resources.

The resource set that is returned represents the resources that are localized for the specified culture. If the resources have not been localized for that culture and tryParents is true, GetResourceSet uses resource fallback rules to load an appropriate resource. If tryParents is false and a culture-specific resource set cannot be found, the method returns null. For more information about resource fallback, see "The Resource Fallback Process" section in the Packaging and Deploying Resources in Desktop Apps article.

The following example calls the GetResourceSet method to retrieve culture-specific resources for the French (France) culture. It then enumerates all of the resources in the resource set. It contains the source code for an executable named ShowNumbers.exe. It also includes the following two text file that contain the names of numbers. The first, NumberResources.txt, contains the names of numbers from one to ten in the English language:


one=one
two=two
three=three
four=four
five=five
six=six
seven=seven
eight=eight
nine=nine
ten=ten

The second, NumberResources.fr-FR.txt, contains the names of numbers from one to four in the French language:


one=un
two=deux
three=trois
four=quatre

You can use a batch file to generate the resource files, embed the English language resource file into the executable, and create a satellite assembly for the French language resources. Here's the batch file to generate an executable by using the Visual Baisc compiler:


resgen NumberResources.txt
vbc shownumbers.vb /resource:NumberResources.resources

md fr-FR
resgen NumberResources.fr-FR.txt
al /out:fr-FR\ShowNumbers.resources.dll /culture:fr-FR /embed:NumberResources.fr-FR.resources

For the C# compiler, you can use the following batch file:


resgen NumberResources.txt
csc shownumbers.cs /resource:NumberResources.resources

md fr-FR
resgen NumberResources.fr-FR.txt
al /out:fr-FR\ShowNumbers.resources.dll /culture:fr-FR /embed:NumberResources.fr-FR.resources

using System;
using System.Globalization;
using System.Resources;

public class Example
{
   public static void Main()
   {
      String[] numbers = { "one", "two", "three", "four", "five", "six",
                           "seven", "eight", "nine", "ten" };
      var rm = new ResourceManager(typeof(NumberResources));
      ResourceSet rs = rm.GetResourceSet(CultureInfo.CreateSpecificCulture("fr-FR"),
                                         true, false);
      if (rs == null) {
         Console.WriteLine("No resource set.");
         return;
      }

      foreach (var number in numbers)
         Console.WriteLine("{0,-10} '{1}'", number + ":", rs.GetString(number));
   }
}

internal class NumberResources {}
// The example displays the following output:
//       one:       'un'
//       two:       'deux'
//       three:     'trois'
//       four:      'quatre'
//       five:      ''
//       six:       ''
//       seven:     ''
//       eight:     ''
//       nine:      ''
//       ten:       ''

Note that if you change the value of the createIfNotExists argument to false, the method call returns null, since Resource Manager has not already loaded the French language resources.

ReflectionPermission

when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2016 Microsoft