read string from .resx file in C#

前端 未结 14 2260
一生所求
一生所求 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: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 resourceFileNameList = new List();
    
           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 "";
       }
    

提交回复
热议问题