How to?

  • Thread starter Thread starter Jacek Jurkowski
  • Start date Start date
J

Jacek Jurkowski

Open file from resource as a stream?
Or just how to read resource text file line by line?
 
If you mean a simply a file marked as an embedded resource, then the
following shows both stream and reader usage. For files in a resx, it is
harder; text files get handled as strings by default... I've never
bothered finding out if you can change this ;-p

Marc

using System;
using System.IO;
using System.Reflection;

class Program
{
static void Main()
{
// need to start at an assembly (and optionally, also a Type)
Assembly assembly = typeof(Program).Assembly;

// what do we have access to?
foreach(string name in assembly.GetManifestResourceNames()) {
Console.WriteLine(name);
}
// watch for the default namespace!
const string FOO_NAME = "MyDefaultNamespace.foo.xml";

// open the stream-reader
using(StreamReader reader = new StreamReader(
assembly.GetManifestResourceStream(FOO_NAME))) {

string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
 
I cant get through ...
assembly.GetManifestResourceStream(FOO_NAME)
whatever I put into FOO_NAME it returna null as a stream
 
Well, what does the first loop display? And are you sure it is an
embedded resource? If it is just "a resource on the file system" then
you can just use File.Open etc... "resource" is an overloaded term, so I
guessed at "embedded".

Marc
 
I mean a main sesource of VS2008 sollution.
Namespace_xxx.Properties.Resources.
The first loop displays this as:

Alicja.SystemAlicja.Properties.Resources.resources
 
That is the "resx" I was referring to... text contents have this
distressing tendency to jump into strings ;-(

You could look at ResourceManager.GetStream, but I've never managed to
get it working (meaning: I was happy to simply use an embedded resource
instead...)

Marc
 
I cant get through ...
  assembly.GetManifestResourceStream(FOO_NAME)
whatever I put into FOO_NAME it returna null as a stream

by default the name of the assembly gets prefixed to the name of the
file
 
ResourceManager returns that fila as a byte[]
not as Stream ... ech ...
 
by default the name of the assembly gets prefixed to the name of the

To be pedantic, I think it uses the default namespace from the project,
not the assembly name. But the two should usually agree...
 
so.

How to "convert" byte[] into a stream or
just recreate file from byte[] chain?
 
That's what I needed :-)

THX

Peter Duniho said:
so.

How to "convert" byte[] into a stream or
just recreate file from byte[] chain?

If none of the other suggestions are working for you, you can always
create a MemoryStream from a byte[].

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top