Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between File.Copy and File.Move

Tags:

c#

file

file-io

Nowadays I am dealing with a small application which updates the mssql's compact database files on an iss server.

I've preferred to use SSIS to organize the flow. For couple of days it worked well, but then started to give errors.

In SSIS I've used the "File System Task"s "Move File" operation to move generated files from a folder to iss server's shared folder. If it fails, in case of a locked file, it tries it later. But I've seen that sometimes the files in the destination folder started to disappear.

Then I've decided to write custom code. I've removed the "File System Task" and put a "Script Task" instead of it. And write a couple of lines in it.

string destinationFile, sourceFile;
destinationFile = Path.Combine(Dts.Variables["FileRemoteCopyLocation"].Value.ToString(), Dts.Variables["CreatedFileName"].Value.ToString());
 sourceFile = Path.Combine(Dts.Variables["OrginalFilePath"].Value.ToString(), Dts.Variables["CreatedFileName"].Value.ToString());


bool written = false;


 try
 {
     File.Copy(sourceFile, destinationFile, true);
     File.Delete(sourceFile);
     written = true;
 }
 catch(IOException) {
    //log it
 }


if (written)
     Dts.TaskResult = (int)ScriptResults.Success;
else
     Dts.TaskResult = (int)ScriptResults.Failure;

It worked well. But I tried it by locking the destination file. I've connected the destination file in Sql Server Management Studio (it is an sdf file). And surprizingly it works too.

And I've tried it from operating system, by copying the source file and pasting it to the destination. Windows 7 asks me if I want to overwrite it and I say yes and it overwrote the file (copy and replace) I use with another process, no warning no error. But if try to rename or delete it does not let me to do that. Or if I try to cut and paste it (Move and Replace) it says "you need permission to do this action".

As I understood, "Copy, delete" and "Move" are totally different things. And I still can not understand how can I overwrite a locked file.

Any ideas?

like image 491
fkucuk Avatar asked Jul 22 '11 12:07

fkucuk


People also ask

What is the difference between copy a file and moving a file?

The main difference between copying and moving is that the copying makes a duplicate of a file or directory in another location without affecting the original content while moving transfers the original file or directory to another location.

What is the difference between copy and move file folders?

1 Answer. Copy a file/folder is to make a duplicate copy of the selected file/folder and place at the target location. Move a file/folder is to transfer the original file from the original location to the target location.

Is move to the same as copy?

Copy is used to create an identical copy. Cut removes the item from the current location. Move to will either rename an object (if it is on the same device) or make an identical copy on another device and removing it from the original so it appears in a different location.


1 Answers

File.Move method can be used to move the file from one path to another. This method works across disk volumes, and it does not throw an exception if the source and destination are the same.

You cannot use the Move method to overwrite an existing file. If you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. To overcome this you can use the combination of Copy and Delete methods

Answer orignal from : Difference between in doing file copy/delete and Move

like image 119
Pranay Rana Avatar answered Sep 30 '22 12:09

Pranay Rana