Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to?

0 views
Skip to first unread message

Jacek Jurkowski

unread,
Apr 22, 2008, 7:25:35 AM4/22/08
to
Open file from resource as a stream?
Or just how to read resource text file line by line?

Alain Boss

unread,
Apr 22, 2008, 7:49:20 AM4/22/08
to
Jacek Jurkowski schrieb:

> Open file from resource as a stream?
> Or just how to read resource text file line by line?

Check out StreamReader()
alain

Marc Gravell

unread,
Apr 22, 2008, 7:59:19 AM4/22/08
to
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);
}
}
}
}

Jacek Jurkowski

unread,
Apr 22, 2008, 9:53:02 AM4/22/08
to
I cant get through ...
assembly.GetManifestResourceStream(FOO_NAME)
whatever I put into FOO_NAME it returna null as a stream

Marc Gravell

unread,
Apr 22, 2008, 9:58:52 AM4/22/08
to
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

Jacek Jurkowski

unread,
Apr 22, 2008, 10:07:38 AM4/22/08
to
I mean a main sesource of VS2008 sollution.
Namespace_xxx.Properties.Resources.
The first loop displays this as:

Alicja.SystemAlicja.Properties.Resources.resources

Marc Gravell

unread,
Apr 22, 2008, 10:21:01 AM4/22/08
to
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

Ignacio Machin ( .NET/ C# MVP )

unread,
Apr 22, 2008, 10:38:59 AM4/22/08
to

by default the name of the assembly gets prefixed to the name of the
file

Jacek Jurkowski

unread,
Apr 22, 2008, 10:39:01 AM4/22/08
to
ResourceManager returns that fila as a byte[]
not as Stream ... ech ...

Marc Gravell

unread,
Apr 22, 2008, 10:45:47 AM4/22/08
to
> by default the name of the assembly gets prefixed to the name of the
> file

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

Jacek Jurkowski

unread,
Apr 22, 2008, 10:55:09 AM4/22/08
to
so.

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

Peter Duniho

unread,
Apr 22, 2008, 1:54:16 PM4/22/08
to

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

Pete

Jacek Jurkowski

unread,
Apr 22, 2008, 3:42:58 PM4/22/08
to
That's what I needed :-)

THX

"Peter Duniho" <NpOeS...@nnowslpianmk.com> wrote in message
news:op.t90xc...@petes-computer.local...

0 new messages