ResXResourceReader.BasePath Property


Gets or sets the base path for the relative file path specified in a ResXFileRef object.

Namespace:   System.Resources
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public string BasePath { get; set; }

Property Value

Type: System.String

A path that, if prepended to the relative file path specified in a ResXFileRef object, yields an absolute path to a resource file.

Exception Condition

In a set operation, a value cannot be specified because the XML resource file has already been accessed and is in use.

The BasePath property is used to resolve relative file path references that are assigned to the FileName property of ResXFileRef objects. By default, its value is String.Empty, and relative file path references are resolved in relationship to the current directory returned by the Environment.CurrentDirectory property. You should set this property before you begin enumerating resources.

The following example creates an XML resource file that contains images of dog breeds, and also creates a string resource that specifies the application that created the resource. ResXFileRef objects are used to store the path to the images rather than storing the binary images themselves in the resource file. The example sets the BasePath property so that the relative file paths in the images' file names are interpreted as subdirectories of a directory named C:\data\.

using System;
using System.Collections;
using System.ComponentModel.Design;
using System.Drawing;
using System.Reflection;
using System.Resources;

public class Example
   public static void Main()

      // Read the resources in the XML resource file.
      ResXResourceReader resx = new ResXResourceReader("DogBreeds.resx"); 
      Console.WriteLine("Default Base Path: '{0}'", resx.BasePath);
      resx.BasePath = @"C:\Data\";
      Console.WriteLine("Current Base Path: '{0}'\n", resx.BasePath); 
      resx.UseResXDataNodes = true;

      IDictionaryEnumerator dict = resx.GetEnumerator();
      AssemblyName[] assemblyNames = { new AssemblyName(typeof(Bitmap).Assembly.FullName) };
      while (dict.MoveNext()) {
         ResXDataNode node = (ResXDataNode) dict.Value;
         if (node.FileRef != null) {
            object image = node.GetValue(assemblyNames);
            Console.WriteLine("{0}: {1} from {2}", dict.Key, 
                              image.GetType().Name, node.FileRef.FileName);
         else {
            Console.WriteLine("{0}: {1}", node.Name, node.GetValue((ITypeResolutionService) null));

   private static void CreateXMLResourceFile()
      // Define an array of ResXFileRef objects for images.
      String typeName = String.Format("{0}, {1}", typeof(Bitmap).FullName, 
      ResXFileRef[] imageRefs =
         { new ResXFileRef(@"images\Akita.jpg", typeName),
           new ResXFileRef(@"images\Dalmatian.jpg", typeName),
           new ResXFileRef(@"images\Husky.jpg", typeName),
           new ResXFileRef(@"images\GreatPyrenees.jpg", typeName),
           new ResXFileRef(@"images\Malamute.jpg", typeName),
           new ResXFileRef(@"images\newfoundland.jpg", typeName),
           new ResXFileRef(@"images\Rottweiler.jpg", typeName) 

      using (ResXResourceWriter resx = new ResXResourceWriter(@".\DogBreeds.resx")) {
         // Add each ResXFileRef object to the resource file.
         foreach (var imageRef in imageRefs) {
            // Form resource name from name of image.
            String name = imageRef.FileName;
            name = name.Substring(name.IndexOf(@"\") + 1);
            name = name.Substring(0, name.IndexOf("."));
            ResXDataNode node = new ResXDataNode(name, imageRef); 
         resx.AddResource("CreatedBy", typeof(Example).Assembly.FullName);
// The example displays the following output:
//    Default Base Path: ''
//    Current Base Path: 'C:\Data\'
//    Akita: Bitmap from C:\Data\images\Akita.jpg
//    Dalmatian: Bitmap from C:\Data\images\Dalmatian.jpg
//    Husky: Bitmap from C:\Data\images\Husky.jpg
//    GreatPyrenees: Bitmap from C:\Data\images\GreatPyrenees.jpg
//    Malamute: Bitmap from C:\Data\images\Malamute.jpg
//    newfoundland: Bitmap from C:\Data\images\newfoundland.jpg
//    Rottweiler: Bitmap from C:\Data\images\Rottweiler.jpg
//    CreatedBy: BasePathEx1, Version=, Culture=neutral, PublicKeyToken=null

Note that you must add a reference to System.Drawing.dll to compile the project successfully. The example also requires that the necessary images exist in a directory named C:\data\images\.

.NET Framework
Available since 2.0
Return to top