日期:2014-05-18  浏览次数:20978 次

如何向WinForm中Resource.resx文件中添加字符串,并读取出来
网上查了一些这方面的资料,可是我向里面添加,它说添加成功!可是打开此文件一看,并不在里面。
当然也读取不出来,可是我手动向里面添加一个字符串,它就能够读取出来。也就是根本没有添加进去,
请各位前辈帮忙解决一下。

------解决方案--------------------
外置资源文件读写片段:
C# code

    // 将 VS2005ImageLibrary 目录下的图片放入一个资源文件
    string VS2005ImageLibraryDir = "D:\\VS2005ImageLibrary";
    void btnResourceTest_Click(object sender, EventArgs e)
    {
      ResXResourceWriter writer = new ResXResourceWriter(Path.Combine(VS2005ImageLibraryDir, "VS2005ImageLibrary.resx"));
      DirectoryInfo di = new DirectoryInfo(VS2005ImageLibraryDir);
      writeDirectory(writer, di);
      writer.Close();
      writer.Dispose();
    }

    void writeDirectory(ResXResourceWriter writer, DirectoryInfo directory)
    {
      FileInfo[] files = directory.GetFiles();
      if (files != null && files.Length > 0)
        writeFiles(writer, files);

      DirectoryInfo[] dirs = directory.GetDirectories();
      if (dirs != null && dirs.Length > 0)
        foreach (DirectoryInfo di in dirs)
        {
          writeDirectory(writer, di);
        }
    }

    void writeFiles(ResXResourceWriter writer, FileInfo[] files)
    {
      Image bitmap = null;
      Icon icon = null;
      string resourceName = null;
      foreach (FileInfo fi in files)
      {
        resourceName = fi.FullName.Substring(3).Replace('\\', '.');
        if (fi.Extension.EndsWith("gif", StringComparison.OrdinalIgnoreCase) ||
          fi.Extension.EndsWith("bmp", StringComparison.OrdinalIgnoreCase) ||
          fi.Extension.EndsWith("png", StringComparison.OrdinalIgnoreCase))
        {
          bitmap = Bitmap.FromFile(fi.FullName);
          writer.AddResource(resourceName, bitmap);
          bitmap.Dispose();
          icon = null;
        }
        if (fi.Extension.EndsWith("ico", StringComparison.OrdinalIgnoreCase))
        {
          icon = new Icon(fi.FullName);
          writer.AddResource(resourceName, icon);
          icon.Dispose();
          icon = null;
        }
      }
    }

    void button37_Click(object sender, EventArgs e)
    {
      ResXResourceReader reader = new ResXResourceReader(Path.Combine(VS2005ImageLibraryDir, "VS2005ImageLibrary.resx"));
      System.Text.StringBuilder sb = new StringBuilder();
      foreach (DictionaryEntry d in reader)
      {
        sb.AppendFormat("name: {0}, type: {1}{2}", d.Key.ToString(), d.Value.GetType().FullName, "\n");
      }
      reader.Close();
      MessageBox.Show(sb.ToString());
    }

------解决方案--------------------
.resx是以 XML 格式存储的,试试xml的读写方法。