8

I have a couple of questions about the difference between this 2 classes and these specific methods, FileIO.FileSystem.CopyFile() and System.IO.File.Copy()

At the simplest level, they both do the same thing when overloaded with sourceFile, destinationFile and bool set to true to overwrite. EG

FileIO.FileSystem.CopyFile(source, destination, True) 
System.IO.File.Copy(source, destination, True)

My two questions are

  1. What are the differences between the 2 with the overload shown because I can't find (or may be I missed the point) anything on the MSDN site.
  2. How do you (the kind person answering) know the differences when it isn't in the MSDN documentation?
ardila
  • 1,277
  • 1
  • 13
  • 24
Dave
  • 8,163
  • 11
  • 67
  • 103
  • 3
    I expect `FileSystem.CopyFile` to be a thin layer that simply hands over to `File.Copy`. There are many such convenience layers in the `Microsoft.VisualBasic` namespace, many to ease VB6 developers into VB.NET. – Oded Jan 08 '13 at 09:44
  • Call it experience... I did some VB6 dev and VBA work (classic ASP) before .NET existed. Then VB.NET came along with C#. I have the knowledge and experience to tell :) – Oded Jan 08 '13 at 09:50
  • So we agree your magic. I wonder though if this makes this more or less preferable though (eg, which method to use, or if in this case, it doesn't matter as they do the same!) – Dave Jan 08 '13 at 09:51
  • The choice boils down to personal choice. In many ways like coding style ;) – Oded Jan 08 '13 at 09:52
  • Possible duplicate of [System.IO Versus VisualBasic.FileIO](http://stackoverflow.com/questions/1317263/system-io-versus-visualbasic-fileio) – ardila Nov 30 '15 at 20:51

4 Answers4

8

A quick look at Microsoft.VisualBasic.dll in reflector shows FileIO.FileSystem.Copy does just hand over to File.Copy after doing a couple of sanity checks (file, directory exists for example) and creating the destination directory if needed.

PaulB
  • 23,264
  • 14
  • 56
  • 75
3

VisualBasic version after some checks calls System.IO.File.Copy, and I find that out by using the dotPeek, dotPeek is .NET decompiler.

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
1

The only difference I can see is that they have potential to raise a different list of exceptions - and I discovered that, I'm afraid, by reading the MSDN documentation :o)

Martin Milan
  • 6,346
  • 2
  • 32
  • 44
  • Well, actually, this is just the point. I'm asking the question whilst playing with it, it appears when using FileIO.FileSystem.CopyFile automatically creates a folder if required, where as the System.IO doesn't. Thank you for the answer though, I know the overloads makes them difference, as does the exceptions, I just wondered if some one was going to say one is better than the other – Dave Jan 08 '13 at 09:50
  • 1
    @Dave - No one is going to say that. They are different, that's all. "Better" tends to be subjective, and you always have to think "better _for_ x", as "better" by itself means nothing. – Oded Jan 08 '13 at 09:51
  • I'm sorry, you're right, by better I meant depreciated or not using the latest approach (and again, I know that you don't have to use the latest for the 'best')! Thank you. – Dave Jan 08 '13 at 09:52
0

After my own research, it does appear to do something not documented.

FileIO.FileSystem.CopyFile(source, destination, true) will create a folder if it doesn't exist, where as System.IO.File.Copy(source, desintation, true) doesn't and throws an exception.

It also appears that when using FileIO.FileSystem.CopyFile(source, destination, true) the reference remains in memory, so when trying to delete the new folder or file, an exception is thrown "...already in use by another process".

Dave
  • 8,163
  • 11
  • 67
  • 103