Bramble
Bramble

Reputation: 1395

How to write to a resource file?

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?

Upvotes: 3

Views: 23171

Answers (4)

Chandrapal Chetan
Chandrapal Chetan

Reputation: 61

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();
                }

Upvotes: 1

Priyank
Priyank

Reputation: 10623

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();
            }
        }

Upvotes: 1

WorldIsRound
WorldIsRound

Reputation: 1554

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();
   }
}

Upvotes: 7

anishMarokey
anishMarokey

Reputation: 11397

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();
  }
}

Upvotes: 1

Related Questions