The code you show has nothing to do with your goal "to open files any extension", but no code has anything to do with that. More exactly, extension do not matter (and, in modern file systems, there is no such thing as "extension", there is only some naming conventions to suggest (only suggest) a "file type"). What does matter, is the file format; and you cannot say "any file format". Of course, you can "open" (after all, what is "open", in this respect) and read anything at all, for example, as an array of bytes, but why?
So, the goal formulated as such, not just realistic or unrealistic, it simply makes no sense. It looks you are lost pretty well.
Now, the signature of your file is not practical, if breaks the symmetry open-close. It will make difficult applying safe practice working with files, which would guarantee closing of file handles:
try {
} finally {
}
You also the homogenous principle in opening the file. Why read-only file is opened with different APIs? It makes the code less supportable. So, to improve this, you should open the file in the construct shown schematically above, via
new FileStream
in both cases. Even better, for read-only case use
System.IO.StreamReader
or
System.IO.BinaryReader
. These classes implement
System.IDisposable
, so you can apply
using
statement for automatic disposable. Please see:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[
^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[
^].
So, your dialog method should only return the file name and readonly flag, not the file.
However, all this would make no sense if you don't do read operation and know exactly what do you want to read. I don't think you know that at this moment. Maybe I just wasted my time for explaining the detail of opening file above?
—SA