Читати англійською Редагувати

Поділитися через


ResXResourceWriter.AddResource Method

Definition

Adds a resource to the list of resources to write.

Overloads

AddResource(String, Object)

Adds a named resource specified as an object to the list of resources to write.

AddResource(String, String)

Adds a string resource to the resources.

AddResource(ResXDataNode)

Adds a named resource specified in a ResXDataNode object to the list of resources to write.

AddResource(String, Byte[])

Adds a named resource specified as a byte array to the list of resources to write.

AddResource(String, Object)

Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs

Adds a named resource specified as an object to the list of resources to write.

C#
public void AddResource(string name, object value);
C#
public void AddResource(string name, object? value);

Parameters

name
String

The name of the resource.

value
Object

The value of the resource.

Implements

Examples

The following example creates a .resx file named CarResources.resx that stores six strings, an icon, and two application-defined objects (two Automobile objects). To store the icon and the Automobile objects, it calls the AddResource(String, Object) method. Note that the Automobile class, which is defined and instantiated in the example, is tagged with the SerializableAttribute attribute.

C#
using System;
using System.Drawing;
using System.Resources;

[Serializable()] public class Automobile
{
   private string carMake;
   private string carModel;
   private int carYear;
   private int carDoors;
   private int carCylinders;

   public Automobile(string make, string model, int year) :
                     this(make, model, year, 0, 0)
   { }

   public Automobile(string make, string model, int year,
                     int doors, int cylinders)
   {
      this.carMake = make;
      this.carModel = model;
      this.carYear = year;
      this.carDoors = doors;
      this.carCylinders = cylinders;
   }

   public string Make {
      get { return this.carMake; }
   }

   public string Model {
      get {return this.carModel; }
   }

   public int Year {
      get { return this.carYear; }
   }

   public int Doors {
      get { return this.carDoors; }
   }

   public int Cylinders {
      get { return this.carCylinders; }
   }
}

public class Example
{
   public static void Main()
   {
      // Instantiate an Automobile object.
      Automobile car1 = new Automobile("Ford", "Model N", 1906, 0, 4);
      Automobile car2 = new Automobile("Ford", "Model T", 1909, 2, 4);
      // Define a resource file named CarResources.resx.
      using (ResXResourceWriter resx = new ResXResourceWriter(@".\CarResources.resx"))
      {
         resx.AddResource("Title", "Classic American Cars");
         resx.AddResource("HeaderString1", "Make");
         resx.AddResource("HeaderString2", "Model");
         resx.AddResource("HeaderString3", "Year");
         resx.AddResource("HeaderString4", "Doors");
         resx.AddResource("HeaderString5", "Cylinders");
         resx.AddResource("Information", SystemIcons.Information);
         resx.AddResource("EarlyAuto1", car1);
         resx.AddResource("EarlyAuto2", car2);
      }
   }
}

Remarks

The resource is not written until Generate is called. The resource that was added must be serializable.

If the resource being added is a string, it is written as a string; otherwise, the resource is serialized and stored in a binary format.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

AddResource(String, String)

Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs

Adds a string resource to the resources.

C#
public void AddResource(string name, string value);
C#
public void AddResource(string name, string? value);

Parameters

name
String

The name of the resource.

value
String

The value of the resource.

Implements

Examples

The following example creates a .resx file named CarResources.resx that stores six strings, an icon, and two application-defined objects (two Automobile objects). To store the strings, it calls the AddResource(String, String) method.

C#
using System;
using System.Drawing;
using System.Resources;

[Serializable()] public class Automobile
{
   private string carMake;
   private string carModel;
   private int carYear;
   private int carDoors;
   private int carCylinders;

   public Automobile(string make, string model, int year) :
                     this(make, model, year, 0, 0)
   { }

   public Automobile(string make, string model, int year,
                     int doors, int cylinders)
   {
      this.carMake = make;
      this.carModel = model;
      this.carYear = year;
      this.carDoors = doors;
      this.carCylinders = cylinders;
   }

   public string Make {
      get { return this.carMake; }
   }

   public string Model {
      get {return this.carModel; }
   }

   public int Year {
      get { return this.carYear; }
   }

   public int Doors {
      get { return this.carDoors; }
   }

   public int Cylinders {
      get { return this.carCylinders; }
   }
}

public class Example
{
   public static void Main()
   {
      // Instantiate an Automobile object.
      Automobile car1 = new Automobile("Ford", "Model N", 1906, 0, 4);
      Automobile car2 = new Automobile("Ford", "Model T", 1909, 2, 4);
      // Define a resource file named CarResources.resx.
      using (ResXResourceWriter resx = new ResXResourceWriter(@".\CarResources.resx"))
      {
         resx.AddResource("Title", "Classic American Cars");
         resx.AddResource("HeaderString1", "Make");
         resx.AddResource("HeaderString2", "Model");
         resx.AddResource("HeaderString3", "Year");
         resx.AddResource("HeaderString4", "Doors");
         resx.AddResource("HeaderString5", "Cylinders");
         resx.AddResource("Information", SystemIcons.Information);
         resx.AddResource("EarlyAuto1", car1);
         resx.AddResource("EarlyAuto2", car2);
      }
   }
}

Note that the Automobile class, which is defined and instantiated in the example, is tagged with the SerializableAttribute attribute.

Remarks

The resource is not written until Generate is called.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

AddResource(ResXDataNode)

Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs

Adds a named resource specified in a ResXDataNode object to the list of resources to write.

C#
public void AddResource(System.Resources.ResXDataNode node);

Parameters

node
ResXDataNode

A ResXDataNode object that contains a resource name/value pair.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

AddResource(String, Byte[])

Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs
Source:
ResXResourceWriter.cs

Adds a named resource specified as a byte array to the list of resources to write.

C#
public void AddResource(string name, byte[] value);
C#
public void AddResource(string name, byte[]? value);

Parameters

name
String

The name of the resource.

value
Byte[]

The value of the resource to add as an 8-bit unsigned integer array.

Implements

Remarks

The resource is not written until Generate is called.

The resource is serialized and stored in a binary format.

Applies to

.NET Framework 4.8.1 та інші версії
Продукт Версії
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10