我遇到的问题是当文件名是值时如何遍历资源文件。我首先尝试使用DictionaryEntry并发现了同样的问题。您能否就如何解决此问题提供一些指导?
具有以下字符串资源值的本地资源文件:
Name |Value |Comment
-----------------------------------------------------------------------------------------------------
MouseImageUrl2.Text |protected/images/tutorial/goodwork.gif |protected/images/tutorial/goodwork.gif
以下操作(使用System.Windows.Forms;):
ResXResourceReader rsxr = new ResXResourceReader(@"C:\Projects\Working\TFS\Source\Help.aspx.resx");
IEnumerator enumerator = rsxr.GetEnumerator();
这是我收到的错误(解决方案位于C:\ Projects \ Team - SE \ Code_PSGDashboard \ Development \ TAS_LanguageProcessor \ TAS_LanguageProcessor):
ResX file Could not find a part of the path 'C:\Projects\Team - SE\Code_PSGDashboard\Development\TAS_LanguageProcessor\TAS_LanguageProcessor\bin\Protected\Images\tutorial\goodwork.gif'. Line 123, position 5. cannot be parsed.
我正在使用DictionaryEntry,但提供的代码确实产生了相同的结果。将抛出相同错误的DictionaryEntry例程是:
ResXResourceReader rsxr = new ResXResourceReader(@"C:\Projects\Working\TFS\Source\Help.aspx.resx");
foreach (DictionaryEntry d in rsxr){}
答案 0 :(得分:0)
我最终使用的解决方案如下:
using System.Collections;
using System.Resources;
/// <summary> Resource item. </summary>
public static class ResourceItem
{
/// <summary>
/// This routine is responsible for getting all the comments from the original files and then
/// adding them to the new resource files.
/// </summary>
/// <param name="inputResX"> The path to the source resx. </param>
/// <param name="outputResX"> The path to the destination resx. </param>
/// <returns> True if changes were made. </returns>
public static bool CopyComments(string inputResX, string outputResX)
{
bool changesMade = false;
// Populate a Hashtable containing the DataNodes in the output file
var output = new Hashtable();
using (var reader = new ResXResourceReader(outputResX))
{
reader.UseResXDataNodes = true;
IEnumerator enumerator = reader.GetEnumerator();
while (enumerator.MoveNext())
{
var entry = (DictionaryEntry)enumerator.Current;
var dataNode = (ResXDataNode)entry.Value;
output.Add(dataNode.Name, dataNode);
}
}
// Search the Hashtable for equivalent DataNodes in the input file
using (var reader = new ResXResourceReader(inputResX))
{
reader.UseResXDataNodes = true;
IEnumerator enumerator = reader.GetEnumerator();
while (enumerator.MoveNext())
{
var entry = (DictionaryEntry)enumerator.Current;
var inputDataNode = (ResXDataNode)entry.Value;
if (output.ContainsKey(inputDataNode.Name))
{
var outputDataNode = (ResXDataNode)output[inputDataNode.Name];
if (!string.IsNullOrEmpty(inputDataNode.Comment) && outputDataNode.Comment != inputDataNode.Comment)
{
// Update the output resx’s comments with the input resx’s comments
outputDataNode.Comment = inputDataNode.Comment;
changesMade = true;
}
}
}
}
if (changesMade)
{
// Write the changes back to the output file
using (var writer = new ResXResourceWriter(outputResX))
{
foreach (DictionaryEntry entry in output)
{
writer.AddResource(entry.Key.ToString(), entry.Value);
}
writer.Generate();
writer.Close();
}
}
return changesMade;
}
}
这允许我编写新的资源文件并将注释复制到新文件并保存。