2013-07-15 21 views
0

正如a previous question提到的,我有一組字符串資源,該方案的生命週期內不會改變,但只能通過在字符串列表昂貴的操作來獲得的:不能在C#應用程序中使用非本地化的資源

public static class Patterns 
{  
    Public static readonly string StateUS = 
     @"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.StateUS) + @")\b"; 

    Public static readonly string Countries = 
     @"\b(?<STATE>" + CharTree.GenerateRegex(FixedLists.Countries) + @")\b"; 

    //..... 
} 

在試圖提高性能,我已經產生用T4,它從文件中讀取列表,並輸出一個資源文件Regex.restext所需字符串:

StatesUS=A(LA(SK|BAM)A|RKANSAS)|B..... 
Countries=UNITED (STATES|KINGDOM)|CA(NAD|MBODI)A..... 

該文件然後被編譯複製到ta rget目錄生成後:

cd $(ProjectDir) 
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\resgen.exe" /compile Regex.restext 
copy /Y Regex.resources "$(TargetDir)MyProject.Resources.Lists.resources" 

但是,當我嘗試加載靜態構造函數中的這些值Patterns

ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("MyProject.Resources.Lists", @".\", null); 
StateUS = rm.GetString("StateUS") 

我收到以下錯誤:

InnerException: System.Resources.MissingManifestResourceException 
Message=Could not find any resources appropriate for the specified culture (or the neutral culture) on disk. 
System.Resources.FileBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark) 
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark) 
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) 

但這是一個文化中立項目 - 資源僅用於存儲。那麼爲什麼我需要指定一種文化呢?我如何/在哪裏可以這樣做?

如果我採取了錯誤的方式來加載我的資源,這將是一個更好的方式來做到這一點?

+0

如果它如此靜態,爲什麼不生成一次,然後複製並粘貼到您的源代碼? –

+0

由於基礎列表在軟件版本之間經常變化。 – Arithmomaniac

回答

1

此異常告訴你的MyProject.Resources.Lists.resources文件未找到。也許文件路徑或文件名本身不正確。我通過創建示例應用程序來驗證您的示例,並且它工作正常。

您應該用Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location替換@".\"以確保您使用的資源文件位置正確。如果您在應用程序內的任何位置設置了Directory.SetCurrentDirectory("D:\\");,則@".\"會在您的資源中查找。

+0

是的......我在測試瀏覽器中運行NUnit測試,它在完全不同的文件夾中運行。很明顯,依靠編譯後複製是行不通的。 – Arithmomaniac

相關問題