wom*_*omp 25
资源管理有一个完整的命名空间:System.Resources.查看ResourceManager类,以及ResXResourceReader和ResXResourceWriter.
http://msdn.microsoft.com/en-us/library/system.resources.aspx
我设法把手放在一个非常古老的调试方法上,当我测试一些与资源相关的东西时,我常常使用它.这应该为你做的伎俩.
public static void UpdateResourceFile(Hashtable data, String path)
{
Hashtable resourceEntries = new Hashtable();
//Get existing resources
ResXResourceReader reader = new ResXResourceReader(path);
if (reader != null)
{
IDictionaryEnumerator id = reader.GetEnumerator();
foreach (DictionaryEntry d in reader)
{
if (d.Value == null)
resourceEntries.Add(d.Key.ToString(), "");
else
resourceEntries.Add(d.Key.ToString(), d.Value.ToString());
}
reader.Close();
}
//Modify resources here...
foreach (String key in data.Keys)
{
if (!resourceEntries.ContainsKey(key))
{
String value = data[key].ToString();
if (value == null) value = "";
resourceEntries.Add(key, value);
}
}
//Write the combined resource file
ResXResourceWriter resourceWriter = new ResXResourceWriter(path);
foreach (String key in resourceEntries.Keys)
{
resourceWriter.AddResource(key, resourceEntries[key]);
}
resourceWriter.Generate();
resourceWriter.Close();
}
Run Code Online (Sandbox Code Playgroud)
Eli*_*ain 10
public static void AddOrUpdateResource(string key, string value)
{
var resx = new List<DictionaryEntry>();
using (var reader = new ResXResourceReader(resourceFilepath))
{
resx = reader.Cast<DictionaryEntry>().ToList();
var existingResource = resx.Where(r => r.Key.ToString() == key).FirstOrDefault();
if (existingResource.Key == null && existingResource.Value == null) // NEW!
{
resx.Add(new DictionaryEntry() { Key = key, Value = value });
}
else // MODIFIED RESOURCE!
{
var modifiedResx = new DictionaryEntry()
{ Key = existingResource.Key, Value = value };
resx.Remove(existingResource); // REMOVING RESOURCE!
resx.Add(modifiedResx); // AND THEN ADDING RESOURCE!
}
}
using (var writer = new ResXResourceWriter(ResxPathEn))
{
resx.ForEach(r =>
{
// Again Adding all resource to generate with final items
writer.AddResource(r.Key.ToString(), r.Value.ToString());
});
writer.Generate();
}
}
Run Code Online (Sandbox Code Playgroud)
如果要保留资源文件中的现有注释,请使用此(基于SirMoreno的代码修改)
public static void UpdateResourceFile(Hashtable data, String path)
{
Hashtable resourceEntries = new Hashtable();
//Get existing resources
ResXResourceReader reader = new ResXResourceReader(path);
reader.UseResXDataNodes = true;
ResXResourceWriter resourceWriter = new ResXResourceWriter(path);
System.ComponentModel.Design.ITypeResolutionService typeres = null;
if (reader != null)
{
IDictionaryEnumerator id = reader.GetEnumerator();
foreach (DictionaryEntry d in reader)
{
//Read from file:
string val = "";
if (d.Value == null)
resourceEntries.Add(d.Key.ToString(), "");
else
{
val = ((ResXDataNode)d.Value).GetValue(typeres).ToString();
resourceEntries.Add(d.Key.ToString(), val);
}
//Write (with read to keep xml file order)
ResXDataNode dataNode = (ResXDataNode)d.Value;
//resourceWriter.AddResource(d.Key.ToString(), val);
resourceWriter.AddResource(dataNode);
}
reader.Close();
}
//Add new data (at the end of the file):
Hashtable newRes = new Hashtable();
foreach (String key in data.Keys)
{
if (!resourceEntries.ContainsKey(key))
{
String value = data[key].ToString();
if (value == null) value = "";
resourceWriter.AddResource(key, value);
}
}
//Write to file
resourceWriter.Generate();
resourceWriter.Close();
}
Run Code Online (Sandbox Code Playgroud)
Womp 做对了 (10x)。
但这里有一段代码保持 XML 文件顺序,在文件末尾添加新内容。(用于源代码控制)
//Need dll System.Windows.Forms
public static void UpdateResourceFile(Hashtable data, String path)
{
Hashtable resourceEntries = new Hashtable();
//Get existing resources
ResXResourceReader reader = new ResXResourceReader(path);
ResXResourceWriter resourceWriter = new ResXResourceWriter(path);
if (reader != null)
{
IDictionaryEnumerator id = reader.GetEnumerator();
foreach (DictionaryEntry d in reader)
{
//Read from file:
string val = "";
if (d.Value == null)
resourceEntries.Add(d.Key.ToString(), "");
else
{
resourceEntries.Add(d.Key.ToString(), d.Value.ToString());
val = d.Value.ToString();
}
//Write (with read to keep xml file order)
resourceWriter.AddResource(d.Key.ToString(), val);
}
reader.Close();
}
//Add new data (at the end of the file):
Hashtable newRes = new Hashtable();
foreach (String key in data.Keys)
{
if (!resourceEntries.ContainsKey(key))
{
String value = data[key].ToString();
if (value == null) value = "";
resourceWriter.AddResource(key, value);
}
}
//Write to file
resourceWriter.Generate();
resourceWriter.Close();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37662 次 |
| 最近记录: |