473,786 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy a file from location

Is it possible to copy a file from one location to another?
eg.
from
C:\Temp\Documen t\TestDoc.doc
to
C:\Deploy\Docum ent\TestDoc.doc
Jul 27 '06 #1
9 8243
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.co m

"Alan T" <al************ *@yahoo.com.auw rote in message
news:ux******** ******@TK2MSFTN GP03.phx.gbl...
Is it possible to copy a file from one location to another?
eg.
from
C:\Temp\Documen t\TestDoc.doc
to
C:\Deploy\Docum ent\TestDoc.doc

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

"Alan T" <al************ *@yahoo.com.auw rote in message
news:ux******** ******@TK2MSFTN GP03.phx.gbl...
Is it possible to copy a file from one location to another?
eg.
from
C:\Temp\Documen t\TestDoc.doc
to
C:\Deploy\Docum ent\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(o riginalPath))
{

byte[] bytes = new byte[BufferLength];

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

while (numBytesToRead 0L)
{
// Read may return anything from 0 to numBytesToRead.
int n = fRStream.Read(b ytes, 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
CopyingFileEven tArgs e = new
CopyingFileEven tArgs(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\Documen t\TestDoc.doc
to
C:\Deploy\Docum ent\TestDoc.doc
Jul 27 '06 #4
simida <yo**********@g mail.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(b ytes, 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.BeginIn voke.

--
Jon Skeet - <sk***@pobox.co m>
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***@pcsreven uecontrol.comwr ote:
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.co m>
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***@pcsreven uecontrol.comwr ote:
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.co m>
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***@pcsreven uecontrol.comwr ote:
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.co m>
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**********@g mail.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.co m>
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
2104
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. Once the compare is complete the file that was copied to a temp location needs to be deleted. I am using the method file.copy(sourcePath, tempPath, true) to copy the file and then file.delete(tempPath) to delete the file. On some of the files...
8
6213
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 Server2 and visa-versa. The 2 server are running 2003. It's a form-based authentication (.NET 1.1) site and I'm using IIS 6 native mode. Both boxes are set to the same workgroup. Since everything is so locked down now, I'm not sure what's the...
0
1074
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 same as the first) I get an error message that the app is not configured to run in Debug mode. I looked at the web.config file, the assembly file, tried tweaking a few things but still can't get the copy to run from the IDE. The only workaround I...
3
4061
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 same time. It says Access Denied. I am using File.Copy ( Source , Destination , true ) command in my code, where Source : the current physical location of the uploaded file
11
2146
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
1407
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 command line parameter as source file path. so the application will copy the source file and past it into destination folder. The source file will be a network machine under same doamin. the destination folder is webserver some hardcoded path.
1
2682
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 and name.***" to copy single files form one location to another, and i have searched through the net for days, and found nothing that will copy a whole DIRECTORY accross, which is what i want. Iv tried dircopy (but that didnt work) This is so...
1
1359
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 select image files from basically anywhere, once they add the file to the archive I will write any metadata that they enter about the file to my database then I want to copy the file to a hidden secure location on the network. I want this location...
10
4965
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 \sitemanager.xml to this location: \\drake\pvt\%USERNAME%\FileZilla-Prefs\ BUTTON 2 - RESTORE PREFS this will do the opposite:
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.