How to get a value from resource file using its key

前端 未结 5 1059
渐次进展
渐次进展 2021-01-11 23:55

How to get a value from resource file using its key

相关标签:
5条回答
  • 2021-01-12 00:21

    There is a much easier way: Namespace.Properties.Resources.FileName -> gets the string of the file-content.

    i.e.: TestProject1.Properties.Resources.MyXmlFile -> direct access to the File in the resources

    0 讨论(0)
  • 2021-01-12 00:31

    In .cs file type your localization namespace:

       Localization.Resources.KeyName
    

    Easy and fast :)

    0 讨论(0)
  • 2021-01-12 00:34
    public string ReadResourceValue(string file, string key)
    
    {
    
        string resourceValue = string.Empty;
        try
        {
    
            string resourceFile = file;
    
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
    
            ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
            // retrieve the value of the specified key
            resourceValue = resourceManager.GetString(key);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            resourceValue = string.Empty;
        }
        return resourceValue;
    }
    
    0 讨论(0)
  • 2021-01-12 00:41

    ResourceManager.GetString or ResourceManager.GetStream, depending on the type of the resource.

    0 讨论(0)
  • 2021-01-12 00:41

    You can get keyvalue from global resource here.

    //TestResource is resource class name.
    
    String keyValue=string.Empty;
    
    keyValue= Resources.TestResource.KeyString;
    
    0 讨论(0)
提交回复
热议问题