Extracting an embedded resource file issue.
-
When running the following code "stream" is null anyone see why? I'm a noob. Is there a better way to get my embedded resources out of my build?
private static void ExtractFile()
{
Assembly Assemb = Assembly.GetExecutingAssembly();
Stream stream = Assemb.GetManifestResourceStream("LockOut_NA.Properties.Resources.ECLock");
FileStream fs = new FileStream(@"c:\ECLock1.htm",FileMode.CreateNew,FileAccess.Write);
StreamReader Reader = new StreamReader(stream);
StreamWriter Writer = new StreamWriter(fs);
Writer.Write(Reader.ReadToEnd());
}Thanks in advance.
-
When running the following code "stream" is null anyone see why? I'm a noob. Is there a better way to get my embedded resources out of my build?
private static void ExtractFile()
{
Assembly Assemb = Assembly.GetExecutingAssembly();
Stream stream = Assemb.GetManifestResourceStream("LockOut_NA.Properties.Resources.ECLock");
FileStream fs = new FileStream(@"c:\ECLock1.htm",FileMode.CreateNew,FileAccess.Write);
StreamReader Reader = new StreamReader(stream);
StreamWriter Writer = new StreamWriter(fs);
Writer.Write(Reader.ReadToEnd());
}Thanks in advance.
I guess the string passed to GetManifestResourceStream is incorrect. Call GetManifestResourceNames to see available embedded resources.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
When running the following code "stream" is null anyone see why? I'm a noob. Is there a better way to get my embedded resources out of my build?
private static void ExtractFile()
{
Assembly Assemb = Assembly.GetExecutingAssembly();
Stream stream = Assemb.GetManifestResourceStream("LockOut_NA.Properties.Resources.ECLock");
FileStream fs = new FileStream(@"c:\ECLock1.htm",FileMode.CreateNew,FileAccess.Write);
StreamReader Reader = new StreamReader(stream);
StreamWriter Writer = new StreamWriter(fs);
Writer.Write(Reader.ReadToEnd());
}Thanks in advance.
This is unlikely to be related to your problem, but using a StreamReader and StreamWriter to save it as a file is bad form. What if your resource contains a zero? What you could do instead is something like this:
public static void CopyStream(Stream source, Stream destination)
{
byte[] buffer = new byte[128];
long previousSourceIndex = source.Position;
int read = source.Read(buffer, 0, buffer.Length);while(read != 0) { destination.Write(buffer, 0, read); read = source.Read(buffer, 0, buffer.Length); } source.Position = previousSourceIndex;
}
Pass the manifest resource stream as the source, and a FileStream as a destination. Don't forget to close the Streams properly when you've finished - a using block would be helpful