Read,write a file binary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrik Malmström

    #1

    Read,write a file binary

    How do I read, write a file binary?
    I want to open, say, file.exe read it in to the program, then write it out
    to file2.exe.
    Like file copy, anyone have a code sample?


  • Henrik Dahl

    #2
    Re: Read,write a file binary

    Patrick,

    File.Copy.

    More general: BinaryReader/BinaryWriter.


    Best regards,

    Henrik Dahl

    "Patrik Malmström" <popsork@popsor k.com> wrote in message
    news:uBVRuGXTDH A.1916@TK2MSFTN GP12.phx.gbl...[color=blue]
    > How do I read, write a file binary?
    > I want to open, say, file.exe read it in to the program, then write it out
    > to file2.exe.
    > Like file copy, anyone have a code sample?
    >
    >[/color]


    Comment

    • Patrik Malmström

      #3
      Re: Read,write a file binary

      I've looked at them, but the exampel, wish I always use aren't very good
      "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> skrev i meddelandet
      news:uAO5aKXTDH A.2256@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Patrick,
      >
      > File.Copy.
      >
      > More general: BinaryReader/BinaryWriter.
      >
      >
      > Best regards,
      >
      > Henrik Dahl
      >
      > "Patrik Malmström" <popsork@popsor k.com> wrote in message
      > news:uBVRuGXTDH A.1916@TK2MSFTN GP12.phx.gbl...[color=green]
      > > How do I read, write a file binary?
      > > I want to open, say, file.exe read it in to the program, then write it[/color][/color]
      out[color=blue][color=green]
      > > to file2.exe.
      > > Like file copy, anyone have a code sample?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Pramod Anchuparayil

        #4
        Re: Read,write a file binary

        Here ya go,

        You will need to clean that up a little bit, but its a works...

        -- Read

        FileStream fs = new FileStream(@"C: \calc.exe", FileMode.OpenOr Create,
        FileAccess.Read );
        byte[] MyData= new byte[fs.Length];
        fs.Read(MyData, 0, System.Convert. ToInt32(fs.Leng th));
        fs.Close();

        -- Write

        FileStream fs1 = new FileStream(@"C: \calc1.exe", FileMode.OpenOr Create,
        FileAccess.Writ e);
        BinaryWriter bw = new BinaryWriter(fs 1);
        bw.Write(MyData );
        bw.Close();
        fs1.Close();



        "Patrik Malmström" <popsork@popsor k.com> wrote in message
        news:ejpeBNXTDH A.2276@TK2MSFTN GP10.phx.gbl...[color=blue]
        > I've looked at them, but the exampel, wish I always use aren't very good
        > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> skrev i meddelandet
        > news:uAO5aKXTDH A.2256@TK2MSFTN GP11.phx.gbl...[color=green]
        > > Patrick,
        > >
        > > File.Copy.
        > >
        > > More general: BinaryReader/BinaryWriter.
        > >
        > >
        > > Best regards,
        > >
        > > Henrik Dahl
        > >
        > > "Patrik Malmström" <popsork@popsor k.com> wrote in message
        > > news:uBVRuGXTDH A.1916@TK2MSFTN GP12.phx.gbl...[color=darkred]
        > > > How do I read, write a file binary?
        > > > I want to open, say, file.exe read it in to the program, then write it[/color][/color]
        > out[color=green][color=darkred]
        > > > to file2.exe.
        > > > Like file copy, anyone have a code sample?
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Henrik Dahl

          #5
          Re: Read,write a file binary

          Pramod,

          Looks OK, but it could benefit from e.g. the following:

          *) Use a BinaryReader for reading.
          *) use using instead of instantiate ... Close. In this case disposing object
          also work in case an exception should be thrown plus it's much more
          declarative by nature.
          *) Use (int) instead of System.Convert. ToInt32.

          Additionally it may be advantageous to use the BitConverter class.


          Best regards,

          Henrik Dahl

          "Pramod Anchuparayil" <pramod_anchu@h otmail.com> wrote in message
          news:e4%23YL3YT DHA.212@TK2MSFT NGP10.phx.gbl.. .[color=blue]
          > Here ya go,
          >
          > You will need to clean that up a little bit, but its a works...
          >
          > -- Read
          >
          > FileStream fs = new FileStream(@"C: \calc.exe", FileMode.OpenOr Create,
          > FileAccess.Read );
          > byte[] MyData= new byte[fs.Length];
          > fs.Read(MyData, 0, System.Convert. ToInt32(fs.Leng th));
          > fs.Close();
          >
          > -- Write
          >
          > FileStream fs1 = new FileStream(@"C: \calc1.exe", FileMode.OpenOr Create,
          > FileAccess.Writ e);
          > BinaryWriter bw = new BinaryWriter(fs 1);
          > bw.Write(MyData );
          > bw.Close();
          > fs1.Close();
          >
          >
          >
          > "Patrik Malmström" <popsork@popsor k.com> wrote in message
          > news:ejpeBNXTDH A.2276@TK2MSFTN GP10.phx.gbl...[color=green]
          > > I've looked at them, but the exampel, wish I always use aren't very good
          > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> skrev i[/color][/color]
          meddelandet[color=blue][color=green]
          > > news:uAO5aKXTDH A.2256@TK2MSFTN GP11.phx.gbl...[color=darkred]
          > > > Patrick,
          > > >
          > > > File.Copy.
          > > >
          > > > More general: BinaryReader/BinaryWriter.
          > > >
          > > >
          > > > Best regards,
          > > >
          > > > Henrik Dahl
          > > >
          > > > "Patrik Malmström" <popsork@popsor k.com> wrote in message
          > > > news:uBVRuGXTDH A.1916@TK2MSFTN GP12.phx.gbl...
          > > > > How do I read, write a file binary?
          > > > > I want to open, say, file.exe read it in to the program, then write[/color][/color][/color]
          it[color=blue][color=green]
          > > out[color=darkred]
          > > > > to file2.exe.
          > > > > Like file copy, anyone have a code sample?
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Jon Skeet

            #6
            Re: Read,write a file binary

            Henrik Dahl <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote:[color=blue]
            > Looks OK, but it could benefit from e.g. the following:
            >
            > *) Use a BinaryReader for reading.[/color]

            What's the benefit there? I believe BinaryReader and BinaryWriter are
            really there to make it easier to read and write primitives, not blocks
            at a time.
            [color=blue]
            > *) use using instead of instantiate ... Close. In this case disposing object
            > also work in case an exception should be thrown plus it's much more
            > declarative by nature.
            > *) Use (int) instead of System.Convert. ToInt32.
            >
            > Additionally it may be advantageous to use the BitConverter class.[/color]

            Why?

            Actually, I'd write it something like:

            using (FileStream input = new FileStream (...))
            {
            using (FileStream output = new FileStream (...))
            {
            byte[] buffer = new byte[16384];
            int len;
            while ( (len=input.Read (buffer, 0, buffer.Length)) > 0)
            {
            output.Write (buffer, 0, len);
            }
            }
            }

            That way you don't need to read the whole lot into memory at a time.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Patrik Malmström

              #7
              Re: Read,write a file binary

              Thanks everyone!


              Comment

              • Henrik Dahl

                #8
                Re: Read,write a file binary


                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.198300 caccd73de698a16 3@news.microsof t.com...[color=blue]
                > Henrik Dahl <ThisYouShouldR emovehdahl@inet .uni2.dk> wrote:[color=green]
                > > Looks OK, but it could benefit from e.g. the following:
                > >
                > > *) Use a BinaryReader for reading.[/color]
                >
                > What's the benefit there? I believe BinaryReader and BinaryWriter are
                > really there to make it easier to read and write primitives, not blocks
                > at a time.[/color]

                Yes, but often people have some order in their bytes where it's possible to
                take advantage of these services.
                [color=blue]
                >[color=green]
                > > *) use using instead of instantiate ... Close. In this case disposing[/color][/color]
                object[color=blue][color=green]
                > > also work in case an exception should be thrown plus it's much more
                > > declarative by nature.
                > > *) Use (int) instead of System.Convert. ToInt32.
                > >
                > > Additionally it may be advantageous to use the BitConverter class.[/color]
                >
                > Why?[/color]

                Same argument as before plus if the current architecture lets him to do it
                at a higher abstration level.
                [color=blue]
                >
                > Actually, I'd write it something like:
                >
                > using (FileStream input = new FileStream (...))
                > {
                > using (FileStream output = new FileStream (...))
                > {
                > byte[] buffer = new byte[16384];
                > int len;
                > while ( (len=input.Read (buffer, 0, buffer.Length)) > 0)
                > {
                > output.Write (buffer, 0, len);
                > }
                > }
                > }
                >
                > That way you don't need to read the whole lot into memory at a time.[/color]

                I agree in your comment, but if this is the real thing to achieve I would
                just use File.Copy. If the point just is to do the copying there's no point
                in neither BinaryReader or BitConverter of course.
                [color=blue]
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet/
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Henrik Dahl

                  #9
                  Re: Read,write a file binary

                  Patrik,

                  Have you installed the .NET framework on the machine where you got this
                  error message?


                  Best regards,

                  Henrik Dahl

                  "Patrik Malmström" <popsork@popsor k.com> wrote in message
                  news:eqy2WmiTDH A.2092@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > I'll tried that code, ´but when I was trying to start calc1.exe, it sayed
                  > that calc1.exe doesn't are a Win32 program, sorrym but I'm no good at C#.
                  > "Pramod Anchuparayil" <pramod_anchu@h otmail.com> skrev i meddelandet
                  > news:e4#YL3YTDH A.212@TK2MSFTNG P10.phx.gbl...[color=green]
                  > > Here ya go,
                  > >
                  > > You will need to clean that up a little bit, but its a works...
                  > >
                  > > -- Read
                  > >
                  > > FileStream fs = new FileStream(@"C: \calc.exe", FileMode.OpenOr Create,
                  > > FileAccess.Read );
                  > > byte[] MyData= new byte[fs.Length];
                  > > fs.Read(MyData, 0, System.Convert. ToInt32(fs.Leng th));
                  > > fs.Close();
                  > >
                  > > -- Write
                  > >
                  > > FileStream fs1 = new FileStream(@"C: \calc1.exe", FileMode.OpenOr Create,
                  > > FileAccess.Writ e);
                  > > BinaryWriter bw = new BinaryWriter(fs 1);
                  > > bw.Write(MyData );
                  > > bw.Close();
                  > > fs1.Close();
                  > >
                  > >
                  > >
                  > > "Patrik Malmström" <popsork@popsor k.com> wrote in message
                  > > news:ejpeBNXTDH A.2276@TK2MSFTN GP10.phx.gbl...[color=darkred]
                  > > > I've looked at them, but the exampel, wish I always use aren't very[/color][/color][/color]
                  good[color=blue][color=green][color=darkred]
                  > > > "Henrik Dahl" <ThisYouShouldR emovehdahl@inet .uni2.dk> skrev i[/color][/color]
                  > meddelandet[color=green][color=darkred]
                  > > > news:uAO5aKXTDH A.2256@TK2MSFTN GP11.phx.gbl...
                  > > > > Patrick,
                  > > > >
                  > > > > File.Copy.
                  > > > >
                  > > > > More general: BinaryReader/BinaryWriter.
                  > > > >
                  > > > >
                  > > > > Best regards,
                  > > > >
                  > > > > Henrik Dahl
                  > > > >
                  > > > > "Patrik Malmström" <popsork@popsor k.com> wrote in message
                  > > > > news:uBVRuGXTDH A.1916@TK2MSFTN GP12.phx.gbl...
                  > > > > > How do I read, write a file binary?
                  > > > > > I want to open, say, file.exe read it in to the program, then[/color][/color][/color]
                  write[color=blue]
                  > it[color=green][color=darkred]
                  > > > out
                  > > > > > to file2.exe.
                  > > > > > Like file copy, anyone have a code sample?
                  > > > > >
                  > > > > >
                  > > > >
                  > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...