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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Now Im really irritated!!

Now Im really irritated!!

Scheduled Pinned Locked Moved C#
helptutorial
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
    Anthony Mushrow
    wrote on last edited by
    #1

    Ok for a while now ive have been tring to save an object from my resources as a file on the hard drive. The object is a byte[]. You may have seen questions regarding this. Recently i have tried to get it into a stream and then save the stream. But i had problems getting it into a stream, i got alot of help for this and am grateful, but after trying: FileStream fs = rm.GetObject("outside") as FileStream; I got rid of the error "specified cast is not valid", and obtained a new one "object reference not set to an instance of the object". In other words, the filestream (fs) is still null, even after making it equal my object. Any ideas, help, large mallets... would be GREATLY GREATLY apreciated. I have beem trying to get this to work for a total of about 10 hours over the past month, but i realy need to get this sorted soon. If you have any ideas on how to save the object without getting it into a stream post them as well. Thanks in advance for putting up with me!

    I M 2 Replies Last reply
    0
    • A Anthony Mushrow

      Ok for a while now ive have been tring to save an object from my resources as a file on the hard drive. The object is a byte[]. You may have seen questions regarding this. Recently i have tried to get it into a stream and then save the stream. But i had problems getting it into a stream, i got alot of help for this and am grateful, but after trying: FileStream fs = rm.GetObject("outside") as FileStream; I got rid of the error "specified cast is not valid", and obtained a new one "object reference not set to an instance of the object". In other words, the filestream (fs) is still null, even after making it equal my object. Any ideas, help, large mallets... would be GREATLY GREATLY apreciated. I have beem trying to get this to work for a total of about 10 hours over the past month, but i realy need to get this sorted soon. If you have any ideas on how to save the object without getting it into a stream post them as well. Thanks in advance for putting up with me!

      I Offline
      I Offline
      iliyang
      wrote on last edited by
      #2

      OK, as I understand, you have a byte array (the bunary representation of some file) in memory and you want to flush it to the disk. If that's the situation, I think I can help. I recently wrote a similar class. Here's the SaveAs method: _bytes is a byte[] array - a private class field.

      public void SaveAs(string filePath)
      {
      	using(FileStream stream = System.IO.File.OpenWrite(filePath))
      	{
      		stream.Write(_bytes, 0, _bytes.Length);
      	}
      }
      
      1 Reply Last reply
      0
      • A Anthony Mushrow

        Ok for a while now ive have been tring to save an object from my resources as a file on the hard drive. The object is a byte[]. You may have seen questions regarding this. Recently i have tried to get it into a stream and then save the stream. But i had problems getting it into a stream, i got alot of help for this and am grateful, but after trying: FileStream fs = rm.GetObject("outside") as FileStream; I got rid of the error "specified cast is not valid", and obtained a new one "object reference not set to an instance of the object". In other words, the filestream (fs) is still null, even after making it equal my object. Any ideas, help, large mallets... would be GREATLY GREATLY apreciated. I have beem trying to get this to work for a total of about 10 hours over the past month, but i realy need to get this sorted soon. If you have any ideas on how to save the object without getting it into a stream post them as well. Thanks in advance for putting up with me!

        M Offline
        M Offline
        Michael Potter
        wrote on last edited by
        #3

        If rm.GetObject("outside") returns a byte[], then how do you expect to cast it to a FileStream. The 'as' operator casts an object if possible and returns null if it can't. The objects cannot be directly converted into each other therefore, fs is null. The easiest stream you can create with a byte[] array is MemoryStream. MemoryStream ms = new MemoryStream((byte[])rm.GetObject("outside")); If your goal is to write a byte[] array to a file then try a pattern like this: byte[] tmp = (byte[])rm.GetObject("outside"); FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew); fs.Write(tmp,0,tmp.Length); fs.Close();

        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