473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Copy a file from location

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
Jul 27 '06 #1
9 8231
Alan,

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alan T" <al*************@yahoo.com.auwrote in message
news:ux**************@TK2MSFTNGP03.phx.gbl...
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

Jul 27 '06 #2
See System.IO.File.Copy().

"Alan T" <al*************@yahoo.com.auwrote in message
news:ux**************@TK2MSFTNGP03.phx.gbl...
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

Jul 27 '06 #3
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
Jul 27 '06 #4
simida <yo**********@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 27 '06 #5
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
Jul 27 '06 #6
Roger <ro***@pcsrevenuecontrol.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 27 '06 #7
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 <ro***@pcsrevenuecontrol.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 28 '06 #8
Additionally, we locked file firstly, than copied the file. So, the
file length didn't changed.

Sincerely,
simida

Jon wrote:
Roger <ro***@pcsrevenuecontrol.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 28 '06 #9
simida <yo**********@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
8
by: Chris Strobel | last post by:
I have 2 web servers where I need to write a PDF file and need to keep them in sync. In others words, If the Server1 is getting called to create the PDF, then it needs to copy the PDF to the other...
0
by: Rich | last post by:
Hello, I have an asp.net app that runs OK on a workstation in debug mode (from the IDE). If I copy the asp.net app to another location or another workstation (which is configured exactly the...
3
by: Shailesh Gajare | last post by:
Hi All, I have creating an ASP.Net application with two web servers. I am uploading a file which is being uploaded on one of the server, I want to copy the uploaded file on the other server at the...
11
by: F. Michael Miller | last post by:
I'd like to copy the listing of a directory (& sub directories) to a text file in vb.net. I'm looking for teh equivilant of the DOS command dir /s > TargetFile.txt. Thanks!
1
by: Bala | last post by:
Hi all, First of all I just want to know this will work out or not. I am going to have a console application on my server. I am going to call that application from my asp.net page. i'll pass...
1
by: NSF12345 | last post by:
In VB6 (from what i can make out it has sevice pack 2 on, though if someone can tell me how to check i will be grateful) I have been using FileCopy "File Location and name.***" , "New file location...
1
by: Todd Sparks | last post by:
I am developing with VB.NET 2005 and I need to copy a file from somewhere on the users machine to a secured location on the server. I am creating an image archive system that will allow users to...
10
by: Jason | last post by:
I want to create a simple program with Two buttons on the form. BUTTON 1 - BACKUP PREFS this will do the following: Copy C:\Documents and Settings\%USERNAME%\Application Data\FileZilla...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.