|
-
Oct 31st, 2006, 04:07 PM
#1
Thread Starter
Member
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:
private void copyBinaryFile()
{
string FILE_NAME = "c:\\testImage.psd";
string FILE_NAME2 = "c:\\testImage2.psd";
// Create the reader for data.
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(FILE_NAME2, FileMode.CreateNew);
BinaryReader r = new BinaryReader(fs);
BinaryWriter w = new BinaryWriter(fs2);
// Read data from File and Write to new file
// This is the part I'm sure isn't right...
System.Byte bt = r.ReadBytes(fs.Length);
w.Write(bt);
r.Close();
w.Close();
}
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
-
Oct 31st, 2006, 04:35 PM
#2
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
-
Oct 31st, 2006, 05:21 PM
#3
Thread Starter
Member
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?
-
Oct 31st, 2006, 07:54 PM
#4
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.
-
Nov 1st, 2006, 07:42 AM
#5
Thread Starter
Member
Re: read and write binary files (copying .jpg files)
-
Nov 1st, 2006, 10:34 PM
#6
Fanatic Member
Re: read and write binary files (copying .jpg files)
code below is to make a another copy of a file
VB Code:
FileStream sr = new FileStream (@"C:\Documents and Settings\spadministrator\Desktop\ugly1.jpg",FileMode.Open);
FileStream sw = new FileStream(@"C:\Documents and Settings\spadministrator\Desktop\ugly3.jpg",FileMode.CreateNew);
byte[] b = new byte[sr.Length];
sr.Read(b, 0, (int)sr.Length);
sw.Write(b, 0, (int)sr.Length);
-
Nov 1st, 2006, 10:34 PM
#7
Fanatic Member
Re: read and write binary files (copying .jpg files)
code below is to make a another copy of a file
VB Code:
FileStream sr = new FileStream (@"C:\Documents and Settings\test\Desktop\ugly1.jpg",FileMode.Open);
FileStream sw = new FileStream(@"C:\Documents and Settings\test\Desktop\ugly3.jpg",FileMode.CreateNew);
byte[] b = new byte[sr.Length];
sr.Read(b, 0, (int)sr.Length);
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|