繁体   English   中英

从资源文件获取字符串

[英]getting string from resource file

我有以下静态类用于在我的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/"; 我无法使用资源字符串访问文件夹,该怎么办?

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

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM