2011-08-19 26 views
0

我有一個啓用了CLR並在VS2010 SP1中使用.Net v4.0的MFC DLL。我已將名爲ReportStrings.resx的新託管資源文件添加到項目的根目錄中。我正在使用下面的代碼來訪問資源。不管我放在ResourceManger構造函數中。當調用rmResources-> GetString(sKey)時,我得到一個FileNotFoundException。例外說「找不到文件」Report.resources「。」我搜索了我的應用程序對Report.resources的任何引用,但沒有找到任何引用。任何幫助將gretaly讚賞。FileNotFoundException ResourceManage :: GetString C++/CLI

感謝, 喬希

public ref class ResourceGetter 
{ 
    static ResourceManager^ rmResources = gcnew ResourceManager("Report.ReportStrings", Assembly::GetExecutingAssembly()); 

    public: 
    static String^ GetResource(String^ sKey) 
    { 
     String^ sReturn = nullptr; 
     String^ sTheme = String::Empty; 

     try 
     { 
     sReturn = rmResources->GetString(sKey); 
     } 
     catch (Exception^ ex) 
     { 
     ex; 
     } 

     return (sReturn == nullptr) ? "[" + sKey + " not found]" : sReturn; 
    } 
}; 
+0

運行程序Ildasm.exe,雙擊該清單,並尋找.mresource。它應該是類似於mumble.ReportStrings.resources,其中* mumble *部分不可從您的問題中推測出來。 –

+0

@Hans Passant - 這是我在清單中找到的:「.mresource public Report.ReportStrings.resources」。這也是Debug目錄中已編譯資源的名稱。 – Navcomp

+0

'無法找到文件'Report.resources'是一個錯字或異常實際上是否說'Report.ReportStrings.resource'?我也會避免像這樣初始化rmResources。最好在某些類的構造函數中這樣做,以確保程序集是正確的。 –

回答

0

好吧,創建幾個測試項目,並從頭開始重建我的應用程序項目後,我似乎找到了問題。如果我初始化我的主文件的應用程序構造函數中的資源,一切正常。因此,例如,我的項目名爲Report,因此我將初始化代碼添加到Report.cpp文件中的CReportApp構造函數中,並且一切正常。感謝您的幫助Hans Passant。我的代碼如下:在你的可執行文件

ResourceGetter.h

#pragma once 
using namespace System; 
using namespace System::Resources; 

public ref class ResourceGetter 
{ 
    static ResourceManager^ rmResources = nullptr; 

    public: 
    static ResourceGetter() 
    { 
    } 

    static void Initialize() 
    { 
     if (rmResources == nullptr) 
     { 
     rmResources = gcnew ResourceManager("Report.ReportStrings", Assembly::GetExecutingAssembly()); 
     ResourceProxy::Initialize(gcnew Mnc::Utilities::GetResourceDelegate(&ResourceGetter::GetResource)); 
     } 
    } 

    static String^ GetResource(String^ sKey) 
    { 
     String^ sReturn = nullptr; 

     try 
     { 
     if (rmResources != nullptr) 
      sReturn = rmResources->GetString(sKey); 
     else 
      System::Diagnostics::Debug::WriteLine("Failed to retrieve resource (" + sKey + "): Returned null."); 
     } 
     catch (Exception^ ex) 
     { 
     ex; 
     } 

     return (sReturn == nullptr) ? "[" + sKey + " not found]" : sReturn; 
    } 
}; 

Report.cpp

CReportApp::CReportApp() 
{ 
    ResourceGetter::Initialize(); 
}