从资源文件中获取字符串

时间:2016-11-02 13:23:41

标签: c# asp.net .net asp.net-mvc

我在mvc应用程序中有以下静态类用于本地化字符串。 我的资源字符串存储在文件夹中,如

  • 资源/ EnglishStrings.resx
  • 资源/ 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;
    }
}

这种方式string filePath = @"~/Resources/";我无法使用资源字符串访问我的文件夹,我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用Server.MapPath将虚拟路径映射到物理路径。 请参阅this MSDN link

var filePath = HttpContext.Current.Server.MapPath(@"~/Resources/");

答案 1 :(得分:0)

var projectPath =  Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string filePath = Path.Combine(projectPath, "Resources");

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