Results 1 to 7 of 7

Thread: read and write binary files (copying .jpg files)

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    read and write binary files (copying .jpg files)

    I'm trying to read one image file and copy it to the same folder under another name.

    Here's my c# Code..

    VB Code:
    1. private void copyBinaryFile()
    2.         {
    3.             string FILE_NAME = "c:\\testImage.psd";
    4.             string FILE_NAME2 = "c:\\testImage2.psd";
    5.  
    6.             // Create the reader for data.
    7.             FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
    8.             FileStream fs2 = new FileStream(FILE_NAME2, FileMode.CreateNew);
    9.             BinaryReader r = new BinaryReader(fs);
    10.             BinaryWriter w = new BinaryWriter(fs2);    
    11.  
    12.             // Read data from File and Write to new file
    13.  
    14.                         // This is the part I'm sure isn't right...
    15.             System.Byte bt = r.ReadBytes(fs.Length);
    16.             w.Write(bt);
    17.  
    18.             r.Close();
    19.             w.Close();
    20.         }

    I'm close. But I'm not sure how to loop through the binary reader and write the binary data to the new file? Eventually, I want to do this over a network and I don't know what the file types will be, so I have to read each file as binary and write it as binary to another folder.

    How would I read the entire file and write the entire file as a new name?

    Thanks

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: read and write binary files (copying .jpg files)

    Have you tried
    Code:
    System.IO.File.Copy("source/oldName", "destination/newName");
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Re: read and write binary files (copying .jpg files)

    I need to use a binary reader & writer. I don't just want to copy.
    This is only part of an app I'm writing.

    I need to send a file over a network from one pc to another.

    Is there a way to do this with a binary reader/writer?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: read and write binary files (copying .jpg files)

    You are misusing the BinaryReader and BinaryWriter classes. They are for reading/writing objects from/to a binary file. You're not dealing with objects, just straight binary data, i.e. a stream of bytes. You should just be using the FileStream objects directly to read and write the data via byte arrays. It's the Read and Write methods of the FileStream class that you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    60

    Re: read and write binary files (copying .jpg files)

    Thanks!

  6. #6
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: read and write binary files (copying .jpg files)

    code below is to make a another copy of a file
    VB Code:
    1. FileStream  sr = new FileStream (@"C:\Documents and Settings\spadministrator\Desktop\ugly1.jpg",FileMode.Open);
    2.             FileStream sw = new FileStream(@"C:\Documents and Settings\spadministrator\Desktop\ugly3.jpg",FileMode.CreateNew);
    3.             byte[] b = new byte[sr.Length];
    4.             sr.Read(b, 0, (int)sr.Length);            
    5.             sw.Write(b, 0, (int)sr.Length);

  7. #7
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: read and write binary files (copying .jpg files)

    code below is to make a another copy of a file
    VB Code:
    1. FileStream  sr = new FileStream (@"C:\Documents and Settings\test\Desktop\ugly1.jpg",FileMode.Open);
    2.             FileStream sw = new FileStream(@"C:\Documents and Settings\test\Desktop\ugly3.jpg",FileMode.CreateNew);
    3.             byte[] b = new byte[sr.Length];
    4.             sr.Read(b, 0, (int)sr.Length);            
    5.             sw.Write(b, 0, (int)sr.Length);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width