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:
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
2009-03-06 16:29:41
Not quite what I meant, but this solved my problem, thank you :)
Sukasa
2009-03-06 16:44:59
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
2009-03-06 17:12:10
Don't pass around filepaths/filename as strings unless you absolutely _must_. Filestreams (or their logical equivalent) are vastly superior.
Greg D
2009-03-06 17:14:11
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
2009-03-07 19:47:04
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
2009-03-09 12:34:06