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. Copy raw file image

Copy raw file image

Scheduled Pinned Locked Moved C#
comquestion
31 Posts 7 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.
  • D Offline
    D Offline
    devvvy
    wrote on last edited by
    #1

    hello How can you copy raw file image into a byte[]? I've tried, File.Open, then read bytes with different encoding - no luck Should I try http://msdn.microsoft.com/en-us/library/a4b8basy.aspx[^] Thanks

    dev

    D L C 3 Replies Last reply
    0
    • D devvvy

      hello How can you copy raw file image into a byte[]? I've tried, File.Open, then read bytes with different encoding - no luck Should I try http://msdn.microsoft.com/en-us/library/a4b8basy.aspx[^] Thanks

      dev

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You don't use an encoding. Encodings interpret the values read. You won't get the actual data from the file. Use System.Io.File.ReadAllBytes[^].

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You don't use an encoding. Encodings interpret the values read. You won't get the actual data from the file. Use System.Io.File.ReadAllBytes[^].

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        D Offline
        D Offline
        devvvy
        wrote on last edited by
        #3

        This won't work - I need raw file image, include header + data http://www.michaelyip.me.uk/projects/SaCF.pdf[^]

        dev

        M D 2 Replies Last reply
        0
        • D devvvy

          This won't work - I need raw file image, include header + data http://www.michaelyip.me.uk/projects/SaCF.pdf[^]

          dev

          M Offline
          M Offline
          Mustafa Ismail Mustafa
          wrote on last edited by
          #4

          ReadAllBytes as the name implies reads all bytes, regardless of what they should be interpreted as. Its up to you to then translate them into whatever it is you wish.

          If the post was helpful, please vote, eh! Current activities: Playing Star Craft II. Don't bother me, eh? Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

          D 1 Reply Last reply
          0
          • M Mustafa Ismail Mustafa

            ReadAllBytes as the name implies reads all bytes, regardless of what they should be interpreted as. Its up to you to then translate them into whatever it is you wish.

            If the post was helpful, please vote, eh! Current activities: Playing Star Craft II. Don't bother me, eh? Now and forever, defiant to the end. What is Multiple Sclerosis[^]?

            D Offline
            D Offline
            devvvy
            wrote on last edited by
            #5

            I ran a simple experiement, read all byte of HelloWorld.exe and write it to second file - it didn't run and OS complained invalid image

            dev

            L T D 3 Replies Last reply
            0
            • D devvvy

              I ran a simple experiement, read all byte of HelloWorld.exe and write it to second file - it didn't run and OS complained invalid image

              dev

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Consider, if such a simple feature did not work then it would not be possible to do any form of data copying under Windows. Try showing your code, there must be an error somewhere.

              It's time for a new signature.

              1 Reply Last reply
              0
              • D devvvy

                I ran a simple experiement, read all byte of HelloWorld.exe and write it to second file - it didn't run and OS complained invalid image

                dev

                T Offline
                T Offline
                Thomas Krojer
                wrote on last edited by
                #7

                How did you write the new file? Post your code snipplet.

                1 Reply Last reply
                0
                • D devvvy

                  hello How can you copy raw file image into a byte[]? I've tried, File.Open, then read bytes with different encoding - no luck Should I try http://msdn.microsoft.com/en-us/library/a4b8basy.aspx[^] Thanks

                  dev

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  what do you mean by "raw file image"? is it a file containing an image (i.e. header plus pixel data)? is it a file containing the raw data of an image (i.e. pixel data only)? is it the "image" of an executaable? you can read( or write) all the bytes of a file at once using File.ReadAllBytes (WriteAllBytes). If you don't want to have them all at once in memory, use a BinaryReader (BinaryWriter). And what do you mean by "encoding"? if it refers to the Encoding class, that one only applies to text. if it refers to the way pixels get represented, that one is called PixelFormat. Please learn to ask questions properly. And don't say "it didn't run and OS complained invalid image", show the code and the exact compile-time or run-time error message(s). :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  D 1 Reply Last reply
                  0
                  • D devvvy

                    This won't work - I need raw file image, include header + data http://www.michaelyip.me.uk/projects/SaCF.pdf[^]

                    dev

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Yeah, it will work since I used it to read and parse PE format executables myself.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    1 Reply Last reply
                    0
                    • D devvvy

                      I ran a simple experiement, read all byte of HelloWorld.exe and write it to second file - it didn't run and OS complained invalid image

                      dev

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Then you did something else wrong in your code. Post the read and write sections and we may be able to tell you where that is.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      • D devvvy

                        hello How can you copy raw file image into a byte[]? I've tried, File.Open, then read bytes with different encoding - no luck Should I try http://msdn.microsoft.com/en-us/library/a4b8basy.aspx[^] Thanks

                        dev

                        C Offline
                        C Offline
                        Covean
                        wrote on last edited by
                        #11

                        If I'm right then he asks for a way to read all the file data plus the file system (NTFS as example) specific information. On a hard disk there are also a file header / footer for every file. Now this data he wants to read (and maybe) write. 1. Handle a file is the wrong approach you have to handle the file system in your case. 2. There is no class (or?) to read/write from/to the hard disk at this i/o level in C#. 3. Take a look at CreateFile. There is a possibility to read the physical disk.

                        Greetings Covean

                        D 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          what do you mean by "raw file image"? is it a file containing an image (i.e. header plus pixel data)? is it a file containing the raw data of an image (i.e. pixel data only)? is it the "image" of an executaable? you can read( or write) all the bytes of a file at once using File.ReadAllBytes (WriteAllBytes). If you don't want to have them all at once in memory, use a BinaryReader (BinaryWriter). And what do you mean by "encoding"? if it refers to the Encoding class, that one only applies to text. if it refers to the way pixels get represented, that one is called PixelFormat. Please learn to ask questions properly. And don't say "it didn't run and OS complained invalid image", show the code and the exact compile-time or run-time error message(s). :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                          D Offline
                          D Offline
                          devvvy
                          wrote on last edited by
                          #12

                          thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet?

                          dev

                          L 1 Reply Last reply
                          0
                          • D devvvy

                            thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet?

                            dev

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            No I do not understand you, you fail to provide necessary information and ask a clear question. And no I did not suggest anything would be possible or impossible, as it is still completely unclear what you want in the first place. Now stop reading things that are not present; and start providing information that is necessary but missing. :mad:

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                            D 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              No I do not understand you, you fail to provide necessary information and ask a clear question. And no I did not suggest anything would be possible or impossible, as it is still completely unclear what you want in the first place. Now stop reading things that are not present; and start providing information that is necessary but missing. :mad:

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                              D Offline
                              D Offline
                              devvvy
                              wrote on last edited by
                              #14

                              Covean did - learn post only if you have something to offer mate

                              dev

                              L T 2 Replies Last reply
                              0
                              • C Covean

                                If I'm right then he asks for a way to read all the file data plus the file system (NTFS as example) specific information. On a hard disk there are also a file header / footer for every file. Now this data he wants to read (and maybe) write. 1. Handle a file is the wrong approach you have to handle the file system in your case. 2. There is no class (or?) to read/write from/to the hard disk at this i/o level in C#. 3. Take a look at CreateFile. There is a possibility to read the physical disk.

                                Greetings Covean

                                D Offline
                                D Offline
                                devvvy
                                wrote on last edited by
                                #15

                                thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet?

                                dev

                                D C 2 Replies Last reply
                                0
                                • D devvvy

                                  Covean did - learn post only if you have something to offer mate

                                  dev

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Please don't remove any messages that have been replied to. It is against the forum guidelines as it results in messy threads. Yours said: thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet? which is what you also replied to Covean !?!?!? X|

                                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                  D 1 Reply Last reply
                                  0
                                  • D devvvy

                                    thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet?

                                    dev

                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #17

                                    He didn't "understand" you. He frickin' guessed at it using the link you posted and your replies in this thread. I took a look at the link and guessed that you were trying to read the entire contents of the file, uninterpreted, because of the HexEdit screen shots in the link and your mentioning of using Encoding to read the file. You made no mention at all of what you were ultimately trying to do with this information. Luc is correct. You still don't know how to ask a question in a format that is answerable, nor do you provide any of the details to questions that would fill in the missing bits of information critical to giving you the correct answer on the first try. You've demonstrated a history of this. People have to constantly guess at what you're talking about.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak

                                    D 1 Reply Last reply
                                    0
                                    • D devvvy

                                      thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet?

                                      dev

                                      C Offline
                                      C Offline
                                      Covean
                                      wrote on last edited by
                                      #18

                                      This can be done in .NET / C# by using PInvoke. But on the other hand I think that accessing the file system directly from C# should be avoided. If you have problems to get started with this, google for "PInvoke" and try to figure out how to call Windows API from within C#. PS: To get a better start with this forum and all those helper/cpians here, please read the pinned forum guidelines at the start and try to think about / rephrase your question. One more sentences could avoid further inquiry.

                                      Greetings Covean

                                      D T 2 Replies Last reply
                                      0
                                      • C Covean

                                        This can be done in .NET / C# by using PInvoke. But on the other hand I think that accessing the file system directly from C# should be avoided. If you have problems to get started with this, google for "PInvoke" and try to figure out how to call Windows API from within C#. PS: To get a better start with this forum and all those helper/cpians here, please read the pinned forum guidelines at the start and try to think about / rephrase your question. One more sentences could avoid further inquiry.

                                        Greetings Covean

                                        D Offline
                                        D Offline
                                        devvvy
                                        wrote on last edited by
                                        #19

                                        Covean wrote:

                                        If you have problems to get started with this, google for "PInvoke" and try to figure out how to call Windows API from within C#.

                                        I understand pinvoke, but why ... that such things done via platform win32 api better...?

                                        dev

                                        C 1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          Please don't remove any messages that have been replied to. It is against the forum guidelines as it results in messy threads. Yours said: thanks yes you understand me - i will post the code but you're suggesting this can't be done in dotnet? which is what you also replied to Covean !?!?!? X|

                                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                          D Offline
                                          D Offline
                                          devvvy
                                          wrote on last edited by
                                          #20

                                          I figured originally i intend to reply to Covean, but instead went to you. To avoid further confusion I removed it (MOVED back to under Covean's thread)

                                          dev

                                          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