|
You read the file into a byte array using a StreamReader of some description, and write it out using a StreamWriter. Use a BackgroundWorker to run this in, and raise progress changes at appropriate intervals.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
As I have said I have tried with streams (StreamReader, FileStream and BinaryStream) and this is causing the file to become corrupt - when word tries to open it, it has to restore it.
This does not seem to happen when doing other file types. I can only assume that there is some 'additional info' stored with the file that File.Copy() is capable of handling, but streams are not.
Any thoughts?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
 here is a code snippet that I use
public static void Copy(string sourcePath, string destPath, bool overwrite, int bufferSize, ProgressBar tmp_pb)
{
if (!overwrite && File.Exists(destPath))
throw new ArgumentException("File already exists.");
if (!Directory.Exists(Path.GetDirectoryName(destPath)))
Directory.CreateDirectory(Path.GetDirectoryName(destPath));
FileStream tmp_fr = new FileStream(sourcePath, FileMode.Open);
FileStream tmp_fw = new FileStream(destPath, FileMode.Create);
long fileSize = new FileInfo(sourcePath).Length;
if (fileSize < bufferSize)
bufferSize = (int)fileSize;
long numOfLoops = (fileSize / bufferSize);
tmp_pb.Step = tmp_pb.Maximum / (int)numOfLoops;
byte[] tmp_data;
for (int a = 0; a < numOfLoops; a++)
{
tmp_data = new byte[bufferSize];
tmp_fr.Read(tmp_data, 0, bufferSize);
tmp_fw.Write(tmp_data, 0, bufferSize);
tmp_pb.PerformStep();
Application.DoEvents();
}
int leftBytes = (int)(fileSize - tmp_fr.Position);
if (leftBytes > 0)
{
tmp_data = new byte[leftBytes];
tmp_fr.Read(tmp_data, 0, leftBytes);
tmp_fw.Write(tmp_data, 0, leftBytes);
}
tmp_fr.Close();
tmp_fw.Flush();
tmp_fw.Close();
tmp_pb.Value = 0;
}
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
My code is basically the same (just different layout, variables etc.)
Are you able to test that with a Office 2007 word document, and then try to to open the file after the copy? - would really appreciate that if you can thanks
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
dude, its a working code...it can even copy entire office Its just a byte game.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
It's all good now, small mistake on my part. Thanks for your time anyway
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
SharpZipLib contains Core.StreamUtils.Copy(some args including progress callback)
You could look how it works (it's quite short) and do something that looks like it..
When copying blocks did you use the right length and did you flush the output stream? I'm sure you did, but, just checking.. forgetting one or both can result in corruption
|
|
|
|
|
Not using flush (don't think I need with code I'm using) but am sure the length is correct. Here's a snippet...
StreamReader sr = new StreamReader("inputfile.docx");
StreamWriter sw = new StreamWriter("outputfile.docx", false);
FileInfo fi = new FileInfo("inputfile.docx");
long total = fi.Length;
byte[] buffer = new byte[10000];
long byteCount = 0;
while(true)
{
int bytesRead = sr.BaseStream.Read(buffer, 0, buffer.Length);
byteCount += bytesRead;
if(bytesRead == 0)
break;
sw.BaseStream.Write(buffer, 0, buffer.Length);
}
sr.Close();
sw.Close();
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
musefan wrote: sw.BaseStream.Write(buffer, 0, buffer.Length);
There's the mistake, see it? You must write the number of bytes read to correctly handle a partially filled buffer.
Alan.
modified on Wednesday, June 3, 2009 7:10 AM
|
|
|
|
|
nice spot, thanks.
I will give it a go but it make perfect sense so I cant see it not working. Just for any others I am going to change that line to...
sw.BaseStream.Write(buffer, 0, bytesRead);
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Shouldn't that be: (untested and I give no guarantee that I'm even making sense)
StreamReader sr = new StreamReader("inputfile.docx");
StreamWriter sw = new StreamWriter("outputfile.docx", false);
FileInfo fi = new FileInfo("inputfile.docx");
long total = fi.Length;
byte[] buffer = new byte[10000];
long byteCount = 0;
while(true)
{
int bytesRead = sr.BaseStream.Read(buffer, 0, buffer.Length);
byteCount += bytesRead;
if(bytesRead == 0)
break;
sw.BaseStream.Write(buffer, 0, bytesRead);
}
sr.Close();
sw.Close(); Last modified: 14mins after originally posted -- changed to answer
|
|
|
|
|
Yeah, Alan has just said too. But thanks for the response. Rookie mistake really, but sometimes it happens. The problem is that it appeared to work fine with other files so I didn't notice there was an error. It was just because Microsoft Word actually noticed crap at the end of the file that pointed it out. So hey, thanks to Microsoft too
Oh, can either you out Alan change the post to be an 'answer' so I can mark as answer please.
Thanks again, to everyone who took an interest
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi,
The use of the Stream reader and writer is a bit odd as you just want the underlying streams. To eliminate the possibility of the reader and writer internal buffers causing a problem you should open the file directly with FileStream.
e.g.
void Copier(String src, String dest) {
using (FileStream srcfile = new FileStream(src, FileMode.Open, FileAccess.Read)) {
using (FileStream destfile = new FileStream(dest, FileMode.CreateNew, FileAccess.Write)){
byte[] buffer = new byte[16 * 1024];
Int32 bytesRead;
while((bytesRead = srcfile.Read(buffer, 0, buffer.Length)) != 0 ){
destfile.Write(buffer, 0, bytesRead);
}
}
}
}
Alan.
|
|
|
|
|
Yeah thanks, this is what I had first (well I was using FileStream) but changed the types to test, StreamReader being the last I tried with... I'll change it back now
Thanks again
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I am using AT command to read sms from Mobile.
I am using 1> AT, 2> AT+CMGF, 3> AT+CSCS, 4> AT+CPMS, 5> AT+CNMI, 6> AT+CMGL commands to read sms. All are working. But when I try to execute the 1> AT command after 2/3 minutes later it is not working? What I have to do to repeat the execution of the AT command?
modified on Wednesday, June 3, 2009 5:42 AM
|
|
|
|
|
when an Internet Explorer is opened and navigated through other links continuously for sometime, back button(i.e., a navigation toolbar to going to previous url) sould be disabled. How can I do this using c#?
|
|
|
|
|
you can use this javascript function:
window.history.forward(1);
but the Back function can always be achieved by right clicking on the page and choosing to go back.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
|
This is one of the question which appears almost every 2-3 days.
Look at this [^]
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
i have a class and i assing this class value then
when i sorted this value or class value, both sorted
example code folowing :
int[] ggg=new int[5];
int[] ggg1=new int[5];
ggg = networks.layers[i].noron[j].sbaglanti;
ggg1 = ggg;
Array.Sort(ggg);
how can i solve this problem ?
regards
|
|
|
|
|
That is because System.Array is a reference type. When you assign ggg1 = ggg , both ggg and ggg1 refer to the same array instance, so calling Array.Sort on either reference will change that instance.
Check out MSDN[^] or search for value types and reference types in Google.
|
|
|
|
|
i understand but if arrays is referans value, how can i solve my problem?
|
|
|
|
|
You typically find a way to clone the source reference. In this case, Array.Copy should do the trick. Bear in mind that if the elements in the array themselves are reference types, then Array.Copy won't clone them.
|
|
|
|
|
That's what i said 10 minutes ago 
|
|
|
|
|
have you tryed :
Array.Copy(ggg, ggg1, 5);
|
|
|
|