Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embedded resources example
public void Main()
{
// Basic text file reading
StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNameSpace.Resources.mydoc.txt"));
string contents = reader.ReadToEnd();
// RTF file
Stream stream = this.GetType().Assembly.GetManifestResourceStream( "YourNameSpace.Resources.mydoc.rtf" );
this.richTextBoxMain.LoadFile( stream, RichTextBoxStreamType.RichText );
// Image
Stream stream = this.GetType().Assembly.GetManifestResourceStream( "YourNameSpace.Resources.myimage.gif" );
Image image = Image.FromStream(stream);
}
// Audio and Video would require a 3rd party library or Windows Media Player to play
// This method takes a string in a path format, such as "/resources/mydoc.txt" and returns it as a string
// (the resource has to be a string)
public static string GetStringFromResource(string path)
{
if (string.IsNullOrEmpty(path))
return "";
path = path.Replace("/", ".");
path = "MyNameSpace" + path;
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
if (stream == null)
return "";
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.