2016-11-02 69 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"); 
相關問題