How to write to a resource file?

20,414

Solution 1

You can make use of the ResourceWriter . I'd also suggest that you make use of the ResourceManager to read from the file.

Code from the link source:

using System;
using System.Resources;

public class WriteResources {
   public static void Main(string[] args) {

  // Creates a resource writer.
  IResourceWriter writer = new ResourceWriter("myResources.resources");

  // Adds resources to the resource writer.
  writer.AddResource("String 1", "First String");

  writer.AddResource("String 2", "Second String");

  writer.AddResource("String 3", "Third String");

  // Writes the resources to the file or stream, and closes it.
  writer.Close();
   }
}

Solution 2

using System;
using System.Resources;

using (ResXResourceWriter resx = new ResXResourceWriter(@"D:\project\files\resourcefile.resx"))
                {
                    resx.AddResource("Key1", "Value");
                    resx.AddResource("Key2", "Value");
                    resx.AddResource("Key3", "Value");
                    
                    resx.Close();
                }

Solution 3

string path = @"c:\temp\contentfilelocation.extension"; //path to resource file location
if (!File.Exists(path)) 
{
    // Create a file to write to.
    using (StreamWriter writer = File.CreateText(path))
            {
                string line = "<name>" + "|" + "<last>";
                writer.WriteLine();
            }
        }

Solution 4

try this

    class Test {
  public static void Main() {
    ResourceWriter rw = new ResourceWriter("English.resources");
    rw.AddResource("Name", "Test");
    rw.AddResource("Ver", 1.0 );
    rw.AddResource("Author", "www.java2s.com");
    rw.Generate();
    rw.Close();
  }
}
Share:
20,414
Bramble
Author by

Bramble

Updated on November 02, 2021

Comments

  • Bramble
    Bramble over 2 years

    If it is possible to read from a source file, like this:

    string fileContent = Resources.Users;
    
    using (var reader = new StringReader(fileContent))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            string[] split = line.Split('|');
            string name = split[0];
            string last = split[1];
    
        }
    }
    

    Then how can you write to the same file?

  • WorldIsRound
    WorldIsRound about 13 years
    Thought the question was on how to write to a file. Your code seems to be displaying the values.
  • Conrad Frix
    Conrad Frix about 13 years
    There's nothing wrong with including the salient content of an article you've linked to. In fact it makes your answer better in the long run because you never know when MSDN will change their urls
  • Priyank
    Priyank about 13 years
    I updated the code, path should be the location of the file you want to write to.