2013-05-24 9 views
16

मैं/linkres साथ एक बाइनरी फ़ाइल को एम्बेड कर रहा हूँ:नहीं कर सकते लोड एम्बेडेड संसाधन

System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly(); 
string[] names = myAssembly.GetManifestResourceNames(); // it is really there by its name "shader.tkb" 
Stream myStream = myAssembly.GetManifestResourceStream(names[0]); 

यह एक

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'shader.tkb' or one of its dependencies. The system cannot find the file specified. ---> System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002) 
--- End of inner exception stack trace --- 
at System.Reflection.RuntimeAssembly.GetResource(RuntimeAssembly assembly, String resourceName, UInt64& length, StackCrawlMarkHandle stackMark, Boolean skipSecurityCheck) 
at System.Reflection.RuntimeAssembly.GetManifestResourceStream(String name, StackCrawlMark& stackMark, Boolean skipSecurityCheck) 
at System.Reflection.RuntimeAssembly.GetManifestResourceStream(String name) 
की ओर जाता है: संकलक तर्क है, लेकिन जब मैं के साथ लोड करने का प्रयास

यहां समस्या क्या है?

उत्तर

33

1 - फ़ाइल की बिल्ड एक्शन एम्बेडेड संसाधन होना चाहिए।
2 - आप केवल संसाधन का नाम निर्दिष्ट नहीं कर सकते हैं।

क्या मेरे लिए काम किया था:: आप संसाधन नाम

Assembly assembly = this.GetType().Assembly; 
assembly.GetManifestResourceStream(
    assembly.GetName().Name + "." + "SubFolderNameIfAny" + ".shader.tkb"); 
+0

विज्ञापन 1) मैं कमांड लाइन पर इसे कैसे निर्दिष्ट करूं? मेरे पास विजुअल स्टूडियो नहीं है। – clamp

+0

आपने सी # नमूना दिया, मुझे यकीन नहीं है कि आप क्या करने की कोशिश कर रहे हैं। –

+5

GetManifestResourceNames() प्रकाश को शेड करता है –

2

परियोजना नाम स्थान ध्यान में रखा जाना करने के लिए आवश्यकता हो सकती है से पहले पूरे विधानसभा नाम निर्दिष्ट करने के लिए है

System.Reflection.Assembly assem = this.GetType().Assembly;   
using(Stream stream = assem.GetManifestResourceStream("Project_A.FolderName.test.txt")) 

कहाँ " Project_A "मेरी परियोजनाएं नामस्थान थीं।
जहां "फ़ोल्डरनाम" फ़ोल्डर मेरी परियोजनाओं में समाधान एक्सप्लोरर है।
जहां "test.txt" एम्बेडेड संसाधन "फ़ोल्डर नाम" फ़ोल्डर में है।

तो मैं प्रयोग किया है:

assem.GetName().Name 

मैं मिलेगा "परियोजना एक" के बदले "Project_A"?!?!?!?

1

आप फ़ाइलों की पूरी सूची (और उनके नाम) प्राप्त करने के लिए अपने असेंबली ऑब्जेक्ट पर GetManifestResourceNames() का उपयोग कर सकते हैं।

मुझे नहीं पता कि क्यों, लेकिन मेरी परियोजना पर मुझे उपफोल्डर नाम शामिल नहीं करना चाहिए। फ़ाइल नाम के साथ असेंबली का बसनाम।

1

This link एम्बेडेड संसाधनों का उपयोग करने के तरीके को समझने में आपकी सहायता करेगा। असीम द्वारा दिया गया समाधान केवल तभी काम करता है जब परियोजना का प्रॉपर्टी के गुणों के 'एप्लिकेशन' टैब में उल्लिखित परियोजना के डिफ़ॉल्ट नामस्थान के समान विधानसभा नाम समान होता है।

क्या किया जा करने के लिए (भले ही विधानसभा नाम परियोजना के डिफ़ॉल्ट नाम स्थान के रूप में ही नहीं है) की जरूरत है:

using System.IO; 
using System.Reflection; 

Assembly myAssembly = Assembly.GetExecutingAssembly(); 
Stream myStream = assembly.GetManifestResourceStream(fullResourceName); 

जहां fullResourceName एम्बेडेड संसाधन आप पहुंचना चाहते हैं की पूरी तरह से योग्य नाम है।

यदि संसाधन (यहां shader.tkb) प्रोजेक्ट के रूट फ़ोल्डर में है, तो fullResourceName = "Default.Namespace.Of.Project.shader.tkb"। यदि संसाधन आपके प्रोजेक्ट फ़ोल्डर में स्थित Resources नामक फ़ोल्डर के अंदर मौजूद है, तो fullResourceName = "Default.Namespace.Of.Project.Resources.shader.tkb"

संबंधित मुद्दे