2016-11-02 72 views
0

我有以下静态类用于本地化我的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/";我不能与资源字符串访问我的文件夹, 我怎样才能做到这一点?

回答

0

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

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

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