18

/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)

ここで何が問題なのですか?

4

4 に答える 4

35

1 - ファイルのビルド アクションは埋め込みリソースである必要があります。
2 - リソース名だけを指定することはできません。リソース名の前にアセンブリ名全体を指定する必要があります

Assembly assembly = this.GetType().Assembly;
assembly.GetManifestResourceStream(
    assembly.GetName().Name + "." + "SubFolderNameIfAny" + ".shader.tkb");
于 2013-05-24T09:50:23.730 に答える
2

プロジェクトの名前空間を考慮する必要がある場合があります。

私にとってうまくいったのは:

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

「Project_A」は私のプロジェクトの名前空間でした。
「FolderName」は、プロジェクトのソリューション エクスプローラーのフォルダーです。
「test.txt」は「FolderName」フォルダーに埋め込まれたリソースです。

私が使用した場合:

assem.GetName().Name

「Project_A」ではなく「Project A」を取得しますか?!?!?!?

于 2015-06-10T21:48:20.763 に答える
1

Assembly オブジェクトで GetManifestResourceNames() を使用して、ファイル (およびその名前) のリスト全体を取得できます。

理由はわかりませんが、私のプロジェクトではサブフォルダー名を含めるべきではありません。ファイル名を含むアセンブリのベース名のみ。

于 2015-07-13T12:08:17.970 に答える