简体   繁体   中英

getting string from resource file

I have following static class for localize strings inside my mvc app. My resource strings are stored inside folder like

  • Resources/EnglishStrings.resx
  • Resources/DeutschStrings.resx
 public static string ReadResourceValue(string lang, string key)
    {
        string file = "";
        if (lang == "en")
        {
            file = "LocalizedUIStrings.en-EN.resx";
        }
        else if (lang == "de")
        {
            file = "LocalizedUIStrings.de-DE.resx";
        }

        //value for our return value
        string resourceValue = string.Empty;
        try
        {
            // specify your resource file name 
            string resourceFile = file;
            // get the path of your file
            //string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            string filePath = @"~/Resources/";
            // create a resource manager for reading from
            //the resx file
            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;
    }
}

this way string filePath = @"~/Resources/"; I cannot access my folder with resource strings, how can I do that?

You can use Server.MapPath to map virtual path to the physical one. See this MSDN link .

var filePath = HttpContext.Current.Server.MapPath(@"~/Resources/");
var projectPath =  Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string filePath = Path.Combine(projectPath, "Resources");

or

string filePath = Path.Combine(System.IO.Path.GetFullPath(@"..\..\"), "Resources");

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