How to write to a resource file?

馋奶兔 提交于 2019-11-30 18:07:35

问题


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?


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



回答2:


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



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


来源:https://stackoverflow.com/questions/5599662/how-to-write-to-a-resource-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!