theiOSDude
theiOSDude

Reputation: 1470

Why is the resource stream always null?

Hopefully something simple but have tried and tried and keep failing. I am attempting to create a Stream object in a C# application that will copy a CSS file to a specific location. The CSS file is embedded in my resources. Regardles of what I have tried the stream object is always null.

Can somebody please point in the right direction by looking at the below?

Thanks :) burrows111

Assembly Assemb = Assembly.GetExecutingAssembly();
Stream stream = Assemb.GetManifestResourceStream(ThisNameSpace.Properties.Resources.ClockingsMapStyle); // NULL!!!!
FileStream fs = new FileStream("to store in this location", FileMode.Create);
StreamReader Reader = new StreamReader(stream);
StreamWriter Writer = new StreamWriter(fs);
Writer.Write(Reader.ReadToEnd());

Upvotes: 0

Views: 2625

Answers (1)

Kioshiki
Kioshiki

Reputation: 991

This works for me:

StreamReader reader;
StreamWriter writer;
Stream stream;
Assembly assembly = Assembly.GetExecutingAssembly();

using (stream = assembly.GetManifestResourceStream("Namespace.Stylesheet1.css"))
using (reader = new StreamReader(stream))
using (writer = new StreamWriter("test.css"))
{
    string content = reader.ReadToEnd();
    writer.Write(content);
    writer.Close();
}     

I tried it in a standard Windows Forms app.

EDIT: The file (Stylesheet1.css) was included as a normal item in the project with a build action of "Embedded Resource".

Upvotes: 2

Related Questions