2

私が立ち往生している問題は、ファイル名が値の場合にリソース ファイルを反復処理する方法です。最初に DictionaryEntry を使用しようとしましたが、同じ問題も見つかりました。この問題を解決する方法について、いくつかのガイダンスを提供できますか?

前提条件:

次の文字列リソース値を持つローカル リソース ファイル:

Name                 |Value                                   |Comment
-----------------------------------------------------------------------------------------------------
MouseImageUrl2.Text  |protected/images/tutorial/goodwork.gif  |protected/images/tutorial/goodwork.gif
  • 永続性: .resx に埋め込まれます
  • タイプ: System.String、mscorlib、Version=4.0.0.0、Culture=neutral、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 に答える