Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I remember coding file access using the Win API about 15 years ago. It was much

ID: 645894 • Letter: I

Question

I remember coding file access using the Win API about 15 years ago. It was much faster than using the FileStream of my language at the time (Delphi).

I spent a while today experimenting in C# under .Net 4.5, and I cannot see a real advantage to using the Win API. Most of the time the standard FileStream is faster, even with its buffering behavior (I know that used to add a substantial overhead a decade ago).

I've studied this research article by Microsoft, but it's over 10 years old now and not very applicable.

So my question is this: when is it advantageous to use the Win API as opposed to the standard FileStream?

Explanation / Answer

When using .NET (particularly recent versions of .NET), a good guideline is to always use the built-in .NET functionality until and unless there is a marked benefit to doing otherwise. In other words: assume .NET is the right way to get the job done until you strongly suspect (or know) that it isn't. Nine times out of ten, the Framework (having been used so widely) will take into account checks and performance improvements that you won't consider.

It's really the same reason people say not to roll your own security solution if you can avoid it - because it's already been done by someone who is probably smarter than you and knows more about it, and other people have already tested it. The same applies to a widespread library like .NET. If someone has already done the work for you, and that work has been vetted by thousands or millions of users, why not go with that until you have a reason not to (such as it not providing the functionality or speeds you need).