2

我遇到的问题是当文件名是值时如何遍历资源文件。我第一次尝试使用 DictionaryEntry 也发现了同样的问题。您能否就如何解决此问题提供一些指导?

先决条件:

具有以下字符串资源值的本地资源文件:

Name                 |Value                                   |Comment
-----------------------------------------------------------------------------------------------------
MouseImageUrl2.Text  |protected/images/tutorial/goodwork.gif  |protected/images/tutorial/goodwork.gif
  • 持久性:嵌入 .resx
  • 类型:System.String,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089

以下操作(使用 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){}
4

1 回答 1

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

这使我可以编写新的资源文件并将注释复制到新的资源文件中并保存。

于 2014-05-15T23:10:10.253 回答