2016-11-02 7 views
0

私は、私のmvcアプリケーション内の文字列をローカライズするために次の静的クラスを持っています。 私のリソース文字列は、このようstring filePath = @"~/Resources/";リソースファイルから文字列を取得する

  • リソース/ 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; 
    } 
} 

のようにフォルダ内に保存されている私は、リソース文字列と私のフォルダにアクセスできません。 、 どうやってやるの?

答えて

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"); 
関連する問題