I'd like to embed some files (text files, maybe graphics) in a C++/CLI project -- preferably the same way I can do in C# project. This might be however impossible, as I found in this post: http://bytes.com/topic/net/answers/571530-loading-markup-xamlreader-load-resource-file#post2240705. This was written three years ago, so maybe now there is some way to do this (in VS2k8)?
Asked
Active
Viewed 7,707 times
3 Answers
13
Under C++/Cli project go to “Properties…”, then look under “Linker”, and then “Input”, you’ll see the list of embedded files under “Embed Managed Resource File”.

Hamish Grubijan
- 10,562
- 23
- 99
- 147
-
5If you add a file here called test.dat, then you can access it like this: Stream^ stm = Assembly::GetExecutingAssembly()->GetManifestResourceStream ("test.dat"); – Tarydon Dec 31 '09 at 17:28
-
1Also, check for nullptr just in case. – Hamish Grubijan Dec 31 '09 at 23:08
2
Go to file properties, General, Item Type, select Compiled Managed Resource
Works in VS2013
To access resource in program:
Stream^ stream = Assembly::GetExecutingAssembly()->GetManifestResourceStream("file.txt");

Ondrej Petrzilka
- 1,449
- 18
- 24
1
This is an embellishment of Tarydon's comment, showing how to save the embedded resource to a file:
using namespace System::IO;
...
String^ tmpFilename = System::IO::Path::GetTempFileName();
try
{
Stream^ readStream = Assembly::GetExecutingAssembly()->GetManifestResourceStream("embedded_file_name.xyz");
if(readStream != nullptr)
{
FileStream^ writeStream = gcnew FileStream(tmpFilename, FileMode::Create);
readStream->CopyTo(writeStream);
readStream->Close();
writeStream->Close(); // Required to flush the buffer & have non-zero filesize
}
}
catch (...)
{
// Do something?
}

dlchambers
- 3,511
- 3
- 29
- 34