Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is the difference between My.Computer.FileSystem and System.IO.File

There is a lot of duplication of functions in the My.Computer.FileSystem and System.IO.File namespaces.

So what exactly is the difference between:

My.Computer.FileSystem.CopyFile(source, dest, True)

and:

System.IO.File.Copy(source, dest, True)

Is there a performance difference? What is everyone's opinion on which which has the edge on read-ability? I personally use the My.Computer Namespace but that is just habit now.

like image 261
Matt Wilko Avatar asked May 03 '11 14:05

Matt Wilko


1 Answers

My.* is simply a set of facade-pattern classes implemented for VB.NET that encompass common System.IO* (and other) operations. There is a very tiny performance hit since you're going through an extra layer of abstraction but you have to decide if it's worth optimizing for that. I would suggest using whichever way makes sense to you and others in your shop.

If you examine the code for My.Computer.FileSystem.CopyFile with .NET Reflector you will see that the method wraps many System.IO classes such as File and Directory and especially the File class' Copy, Move and Delete methods. Snippet:

'lots of other code snipped out for brevity and to show the use of System.IO classes...

Directory.CreateDirectory(FileSystem.GetParentPath(str))

   'snip

    If 
       ' snip
    Else
        File.Delete(str)
        File.Move(path, str)
    End If
Else
    File.Move(path, str)
End If
End Sub
like image 168
Paul Sasik Avatar answered Nov 16 '22 01:11

Paul Sasik