tags:

views:

105

answers:

2

I'm working on a VB.NET DLL right now, and one of the functions I'm writing is supposed to take a file, and clip out an array of bytes (the method I've got works, and can be abstracted away here). What's the best way to pass a file to this function? Would passing a filename to it be best, or what would be the best way to accomplish this?

+2  A: 

FileStream Class

sample from the link above

Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)
    Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)
    fs.Write(info, 0, info.Length)
End Sub
Fredou
Not quite what I meant, but this solved my problem, thank you :)
Sukasa
A: 

Filenames as a string are an old school standard method of dealing with files. Its a parameter that people know how to set correctly and use.

I agree with Fredou above and Greg D that a FileStream is the best way to go. They are just easier to do things the right way with.

tooleb
Don't pass around filepaths/filename as strings unless you absolutely _must_. Filestreams (or their logical equivalent) are vastly superior.
Greg D
Lots of reasons. The elimination of the File.Exists race condition. The established utility of a FileStream (how should the API deal with access denied on an invalid path, e.g.? If it has to throw an exception anyway, let the API user deal with it directly). Filestreams are inherently more valid.
Greg D
I forgot to mention that passing a filestream makes the API more amenable to later being passed just a Stream. That way users can send data in from any sort of stream, not just a file, and really get some general use out of the API for practically no additional cost.
Greg D