How to?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacek Jurkowski

    #1

    How to?

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

    #2
    Re: How 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

    Comment

    • Marc Gravell

      #3
      Re: How 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.Reflecti on;

      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.GetMan ifestResourceNa mes()) {
      Console.WriteLi ne(name);
      }
      // watch for the default namespace!
      const string FOO_NAME = "MyDefaultNames pace.foo.xml";

      // open the stream-reader
      using(StreamRea der reader = new StreamReader(
      assembly.GetMan ifestResourceSt ream(FOO_NAME)) ) {

      string line;
      while ((line = reader.ReadLine ()) != null)
      {
      Console.WriteLi ne(line);
      }
      }
      }
      }

      Comment

      • Jacek Jurkowski

        #4
        Re: How to?

        I cant get through ...
        assembly.GetMan ifestResourceSt ream(FOO_NAME)
        whatever I put into FOO_NAME it returna null as a stream

        Comment

        • Marc Gravell

          #5
          Re: How 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

          Comment

          • Jacek Jurkowski

            #6
            Re: How to?

            I mean a main sesource of VS2008 sollution.
            Namespace_xxx.P roperties.Resou rces.
            The first loop displays this as:

            Alicja.SystemAl icja.Properties .Resources.reso urces

            Comment

            • Marc Gravell

              #7
              Re: How 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

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: How to?

                On Apr 22, 9:53 am, "Jacek Jurkowski" <jjurkow...@dat a-comp.euwrote:
                I cant get through ...
                  assembly.GetMan ifestResourceSt ream(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

                Comment

                • Jacek Jurkowski

                  #9
                  Re: How to?

                  ResourceManager returns that fila as a byte[]
                  not as Stream ... ech ...

                  Comment

                  • Marc Gravell

                    #10
                    Re: How 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...

                    Comment

                    • Jacek Jurkowski

                      #11
                      Re: How to?

                      so.

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

                      Comment

                      • Peter Duniho

                        #12
                        Re: How to?

                        On Tue, 22 Apr 2008 07:55:09 -0700, Jacek Jurkowski
                        <jjurkowski@dat a-comp.euwrote:
                        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

                        Comment

                        • Jacek Jurkowski

                          #13
                          Re: How to?

                          That's what I needed :-)

                          THX

                          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                          news:op.t90xcqn z8jd0ej@petes-computer.local. ..
                          On Tue, 22 Apr 2008 07:55:09 -0700, Jacek Jurkowski
                          <jjurkowski@dat a-comp.euwrote:
                          >
                          >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
                          >

                          Comment

                          Working...