简体   繁体   中英

How to get the comment from a .resx file entry

Strings in resource files have a name, a value and a comment
The ResXResourceReader class gives me access to the name and the value.
Is there a way to read the comment?

You should be able to get Comment via ResXDataNode class: http://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx

You will need to set UseResXDataNodes flag on the reader: http://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.useresxdatanodes.aspx

Necromancing.
If the link goes dark - with UseResXDataNodes = true :

public static string ReadRessourceFile()
{
    string[] languages = new string[] { "de", "fr", "it", "en" };
    string pathPattern = System.AppDomain.CurrentDomain.BaseDirectory;
    pathPattern = System.IO.Path.Combine(pathPattern, "..", "..", "..", "libQrCodeGenerator", "Resources", "QRBillText-{0}.resx");
    pathPattern = System.IO.Path.GetFullPath(pathPattern);

    System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>> dict = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>(System.StringComparer.InvariantCultureIgnoreCase);

    foreach (string lang in languages)
    {
        dict[lang] = new System.Collections.Generic.Dictionary<string, string>(System.StringComparer.InvariantCultureIgnoreCase);

        string file = string.Format(pathPattern, lang);
        System.Resources.ResXResourceReader rr = new System.Resources.ResXResourceReader(file);
        rr.UseResXDataNodes = true;

        // '# Iterate through the resources and display the contents to the console. 
        foreach (System.Collections.DictionaryEntry d in rr)
        {
            System.Resources.ResXDataNode node = (System.Resources.ResXDataNode)d.Value;
            string value = (string) node.GetValue((System.ComponentModel.Design.ITypeResolutionService)null);
            string comment = node.Comment;

            if(!string.IsNullOrEmpty(comment))
            {
                System.Console.WriteLine(comment);
            }

            // dict[lang][d.Key.ToString()] = d.Value.ToString(); // when not using UseResXDataNodes = true
            dict[lang][d.Key.ToString()] = value;
        }

        // '#Close the reader. 
        rr.Close();
    }

    string json = Newtonsoft.Json.JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented);
    return json;
}

This way seems to work ONLY for .RESX file on disk. If you usethe embedded version, comments seems to be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM