Created
February 14, 2013 23:12
-
-
Save yetanotherchris/4957257 to your computer and use it in GitHub Desktop.
Embedded resources example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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