Copy a file from location

A

Alan T

Is it possible to copy a file from one location to another?
eg.
from
C:\Temp\Document\TestDoc.doc
to
C:\Deploy\Document\TestDoc.doc
 
N

Nicholas Paldino [.NET/C# MVP]

Alan,

Use the static Copy method on the File class in the System.IO namespace.

Hope this helps.
 
S

simida

Yes, But if you want to copy a big file.you should do use buffer to
copy file and retrieve information about copied file size to notify UI
progress bar.

/*----------------------- core code --------------------*/

public void Copy(string originalPath, string destinationPath)
{
using (FileStream fRStream = File.OpenRead(originalPath))
{

byte[] bytes = new byte[BufferLength];

long numBytesToRead = fRStream.Length;
using (FileStream fWStream = File.Create(destinationPath))
{

while (numBytesToRead > 0L)
{
// Read may return anything from 0 to numBytesToRead.
int n = fRStream.Read(bytes, 0, bytes.Length);
// The end of the file is reached.
if (n == 0)
{
break;
}
if (n == bytes.Length)
{
// Write data the destination file.
fWStream.Write(bytes, 0, bytes.Length);
}
else
{
fWStream.Write(bytes, 0, n);
}
numBytesToRead -= (long)n;

// Notify observer
CopyingFileEventArgs e = new
CopyingFileEventArgs(fRStream.Length, fRStream.Length - numBytesToRead,
n);
OnCopying(e);
}
fWStream.Flush();
fWStream.Close();
}
}
 
J

Jon Skeet [C# MVP]

simida said:
Yes, But if you want to copy a big file.you should do use buffer to
copy file and retrieve information about copied file size to notify UI
progress bar.

A few comments about your code:

1) Why bother using the file's length to start with? Just keep reading
until Stream.Read returns 0. That can make the code simple:

int bytesRead;
while ( (bytesRead = fRStream.Read(bytes, 0, bytes.Length)) != 0)
{
...
}

There's no need to break inside the loop then.

2)
if (n == bytes.Length)
{
// Write data the destination file.
fWStream.Write(bytes, 0, bytes.Length);
}
else
{
fWStream.Write(bytes, 0, n);
}

This is equivalent to:

fWStream.Write (bytes, 0, n);

3) There's no need to explicitly flush and close a stream if you've got
it within a using statement.

4) This should be performed on a non-UI thread - which means that the
OnCopying event will need to marshall over to the UI thread to report
progress, using Control.Invoke or Control.BeginInvoke.
 
R

Roger

copy file and retrieve information about copied file size to notify UI
A few comments about your code:

1) Why bother using the file's length to start with?

This info could be used to show a Progress Bar,
percentage complete.
Roger
 
J

Jon Skeet [C# MVP]

Roger said:
This info could be used to show a Progress Bar,
percentage complete.

Potentially, I suppose. It's worth bearing in mind that the file could
grow while it's being copied, however.
 
S

simida

Thinks your comments . Jon.

This is just a part of Coping file and notify UI. OnCopy event
subscriber and Copy method reside in the same Woker thread. I used
Control.Invoke method to notify UI there.

yes, i know your comment 1) while(......) method, but i think my method
can let other people to know it quickly. file length and other
information are used to notify UI percentage.

Sincerely,
simida
 
S

simida

Additionally, we locked file firstly, than copied the file. So, the
file length didn't changed.

Sincerely,
simida
 
J

Jon Skeet [C# MVP]

simida said:
Additionally, we locked file firstly, than copied the file. So, the
file length didn't changed.

That may be the case for where you used the code in your system, but
you're presenting the code for someone else to use, without presenting
that extra information.

I would suggest that if the original length of the file *is* desired
information, it would be better to retrieve it at the start and
remember it *purely* for the sake of giving the UI a hint (which it
should only treat as a hint) and then use the more robust way of
reading until the stream ends.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top