Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extracting an embedded resource file issue.

Extracting an embedded resource file issue.

Scheduled Pinned Locked Moved C#
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astroudjr
    wrote on last edited by
    #1

    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.

    G 0 2 Replies Last reply
    0
    • A astroudjr

      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.

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A astroudjr

        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.

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups