read string from .resx file in C#

前端 未结 14 2098
一生所求
一生所求 2020-12-02 06:01

How to read the string from .resx file in c#? please send me guidelines . step by step

相关标签:
14条回答
  • 2020-12-02 06:24

    The easiest way to do this is:

    1. Create an App_GlobalResources system folder and add a resource file to it e.g. Messages.resx
    2. Create your entries in the resource file e.g. ErrorMsg = This is an error.
    3. Then to access that entry: string errormsg = Resources.Messages.ErrorMsg
    0 讨论(0)
  • 2020-12-02 06:26

    Once you add a resource (Name: ResourceName and Value: ResourceValue) to the solution/assembly, you could simply use "Properties.Resources.ResourceName" to get the required resource.

    0 讨论(0)
  • 2020-12-02 06:32

    Open .resx file and set "Access Modifier" to Public.

    var <Variable Name> = Properties.Resources.<Resource Name>
    
    0 讨论(0)
  • 2020-12-02 06:33

    Try this, works for me.. simple

    Assume that your resource file name is "TestResource.resx", and you want to pass key dynamically then,

    string resVal = TestResource.ResourceManager.GetString(dynamicKeyVal);
    

    Add Namespace

    using System.Resources;
    
    0 讨论(0)
  • 2020-12-02 06:33

    Followed by @JeffH answer, I recommend to use typeof() than string assembly name.

        var rm = new ResourceManager(typeof(YourAssembly.Properties.Resources));
        string message = rm.GetString("NameOfKey", CultureInfo.CreateSpecificCulture("ja-JP"));
    
    0 讨论(0)
  • 2020-12-02 06:39

    If for some reason you can't put your resources files in App_GlobalResources, then you can open resources files directly using ResXResourceReader or an XML Reader.

    Here's sample code for using the ResXResourceReader:

       public static string GetResourceString(string ResourceName, string strKey)
       {
    
    
           //Figure out the path to where your resource files are located.
           //In this example, I'm figuring out the path to where a SharePoint feature directory is relative to a custom SharePoint layouts subdirectory.  
    
           string currentDirectory = Path.GetDirectoryName(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"]));
    
           string featureDirectory = Path.GetFullPath(currentDirectory + "\\..\\..\\..\\FEATURES\\FEATURENAME\\Resources");
    
           //Look for files containing the name
           List<string> resourceFileNameList = new List<string>();
    
           DirectoryInfo resourceDir = new DirectoryInfo(featureDirectory);
    
           var resourceFiles = resourceDir.GetFiles();
    
           foreach (FileInfo fi in resourceFiles)
           {
               if (fi.Name.Length > ResourceName.Length+1 && fi.Name.ToLower().Substring(0,ResourceName.Length + 1) == ResourceName.ToLower()+".")
               {
                   resourceFileNameList.Add(fi.Name);
    
               }
            }
    
           if (resourceFileNameList.Count <= 0)
           { return ""; }
    
    
           //Get the current culture
           string strCulture = CultureInfo.CurrentCulture.Name;
    
           string[] cultureStrings = strCulture.Split('-');
    
           string strLanguageString = cultureStrings[0];
    
    
           string strResourceFileName="";
           string strDefaultFileName = resourceFileNameList[0];
           foreach (string resFileName in resourceFileNameList)
           {
               if (resFileName.ToLower() == ResourceName.ToLower() + ".resx")
               {
                   strDefaultFileName = resFileName;
               }
    
               if (resFileName.ToLower() == ResourceName.ToLower() + "."+strCulture.ToLower() + ".resx")
               {
                   strResourceFileName = resFileName;
                   break;
               }
               else if (resFileName.ToLower() == ResourceName.ToLower() + "." + strLanguageString.ToLower() + ".resx")
               {
                   strResourceFileName = resFileName;
                   break;
               }
           }
    
           if (strResourceFileName == "")
           {
               strResourceFileName = strDefaultFileName;
           }
    
    
    
           //Use resx resource reader to read the file in.
           //https://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.aspx
    
           ResXResourceReader rsxr = new ResXResourceReader(featureDirectory + "\\"+ strResourceFileName);         
    
           //IDictionaryEnumerator idenumerator = rsxr.GetEnumerator();
           foreach (DictionaryEntry d in rsxr)
           {
               if (d.Key.ToString().ToLower() == strKey.ToLower())
               {
                   return d.Value.ToString();
               }
           }
    
    
           return "";
       }
    
    0 讨论(0)
提交回复
热议问题