PC Review


Reply
Thread Tools Rate Thread

Copy a file from location

 
 
Alan T
Guest
Posts: n/a
 
      27th Jul 2006
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


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      27th Jul 2006
Alan,

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

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Alan T" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Brendan Green
Guest
Posts: n/a
 
      27th Jul 2006
See System.IO.File.Copy().

"Alan T" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
simida
Guest
Posts: n/a
 
      27th Jul 2006
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();
}
}

Alan T wrote:
> 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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Jul 2006
simida <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      27th Jul 2006
> > 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?


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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Jul 2006
Roger <(E-Mail Removed)> wrote:
> > > 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?

>
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
simida
Guest
Posts: n/a
 
      28th Jul 2006
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


Jon wrote:
> Roger <(E-Mail Removed)> wrote:
> > > > 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?

> >
> > 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.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too


 
Reply With Quote
 
simida
Guest
Posts: n/a
 
      28th Jul 2006
Additionally, we locked file firstly, than copied the file. So, the
file length didn't changed.

Sincerely,
simida

Jon wrote:
> Roger <(E-Mail Removed)> wrote:
> > > > 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?

> >
> > 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.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      28th Jul 2006
simida <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy DB file to new location, and open without warnings Presto Microsoft Access Form Coding 9 5th Apr 2009 01:28 AM
I cant open a word file unless I copy it to another location =?Utf-8?B?RXJkZW01NTg=?= Microsoft Word Document Management 0 11th Mar 2007 12:29 PM
File copy to secure location. Todd Sparks Microsoft VB .NET 1 16th Feb 2007 08:13 AM
Copy file from http location JohnSouth Microsoft C# .NET 3 26th Apr 2006 05:47 PM
Use vb line to copy a dbf file to another location mj Microsoft Access VBA Modules 2 9th Mar 2004 10:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:24 AM.