如何写入资源文件?

时间:2011-04-08 19:04:30

标签: c# file embedded-resource

如果可以从源文件中读取,请执行以下操作:

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];

    }
}

然后你怎么写同一个文件?

3 个答案:

答案 0 :(得分:6)

您可以使用ResourceWriter。 我还建议您使用ResourceManager从文件中读取。

来自链接源的代码:

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

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:1)

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