|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FILE I/O Inheritance? Interface?I have a need to create, I guess in .NET terms, a provider for a file I/O interface. For example, in VB.NET ' open local file to read Dim sr As New BinaryReader(File.Open("c:\file.txt", FileMode.open)) ' open server file to write Dim sw As New BinaryWriter(File.Open("wc:\123.txt", fileMode.write)) where the source and target streams is prepared based on the prefix of the file name. I want to inherit or wrap the FILE class to handle proprietary I/O. I have this done already in C/C++ and would like to do the same, if possible, in VB.NET. Should/Can I use BinaryReader, BinaryWriter, File or some other class like Stream, BufferedStream? In other tools framework, they provides a FILE I/O class where you override the the basic methods: Open Read Write Seek Lock UnLock EOF Close etc. How can this be done in .NET? Thanks -- Hi Mike,
How about deriving a class from Stream? http://msdn.microsoft.com/en-us/library/system.io.stream.aspx Nick. Show quoteHide quote "Mike" <unkn***@unknown.tv> wrote in message news:OZRTtFN6JHA.5180@TK2MSFTNGP04.phx.gbl... > Folks, > > I have a need to create, I guess in .NET terms, a provider for a file I/O > interface. > For example, in VB.NET > > ' open local file to read > Dim sr As New BinaryReader(File.Open("c:\file.txt", FileMode.open)) > > ' open server file to write > Dim sw As New BinaryWriter(File.Open("wc:\123.txt", fileMode.write)) > > where the source and target streams is prepared based on the prefix of the > file name. > > I want to inherit or wrap the FILE class to handle proprietary I/O. > I have this done already in C/C++ and would like to do the same, if > possible, in VB.NET. > > Should/Can I use BinaryReader, BinaryWriter, File or some other class like > Stream, BufferedStream? > > In other tools framework, they provides a FILE I/O class where you > override the the basic methods: > > Open > Read > Write > Seek > Lock > UnLock > EOF > Close > > etc. How can this be done in .NET? > > Thanks > > -- > > > > > On 2009-06-09, Mike <unkn***@unknown.tv> wrote:
Show quoteHide quote > Folks, Inherit from System.IO.Stream.> > I have a need to create, I guess in .NET terms, a provider for a file > I/O interface. > For example, in VB.NET > > ' open local file to read > Dim sr As New BinaryReader(File.Open("c:\file.txt", FileMode.open)) > > ' open server file to write > Dim sw As New BinaryWriter(File.Open("wc:\123.txt", fileMode.write)) > > where the source and target streams is prepared based on the prefix of > the file name. > > I want to inherit or wrap the FILE class to handle proprietary I/O. > I have this done already in C/C++ and would like to do the same, if > possible, in VB.NET. > > Should/Can I use BinaryReader, BinaryWriter, File or some other class > like Stream, BufferedStream? > > In other tools framework, they provides a FILE I/O class where you > override the the basic methods: > > Open > Read > Write > Seek > Lock > UnLock > EOF > Close > > etc. How can this be done in .NET? > > Thanks -- Tom Shelton Tom Shelton wrote:
Show quoteHide quote >> In other tools framework, they provides a FILE I/O class where you Nak and Tom:>> override the the basic methods: >> >> Open >> Read >> Write >> Seek >> Lock >> UnLock >> EOF >> Close >> >> etc. How can this be done in .NET? >> >> Thanks > > Inherit from System.IO.Stream. This is the approach I started, but I am still missing something, namely the open file or file related overrides so I am checking that out. Maybe that is what the CreateObjRef() is? I can do it by directly using our WcCreateFile() function and passing the handle to the Stream class. Then all the other read, write, positioning functions apply. In short, I have a fascimile of the WIN32 functions: HANDLE WcCreateFile(const char * fn, ....); BOOL WcCloseHandle(HANDLE h); BOOL WcWriteFile(WCHANDLE h, LPCVOID buffer, ....); BOOL WcReadFile(WCHANDLE h, LPVOID buffer, DWORD requested, ...); BOOL WcSetEndOfFile(WCHANDLE h); DWORD WcSetFilePointer(WCHANDLE h, LONG dist, DWORD movemethod); BOOL WcSetFileTime(WCHANDLE h, FILETIME &ft); DWORD WcGetFileSize(WCHANDLE h); BOOL WcGetFileTime(WCHANDLE h, FILETIME &ft); And the Read/Write, Pointer functions apply to the Stream class, but the others need to be at higher level. That will allow for: dim stm as new stream = WCFile.Open("wc:\123.txt", fileMode.write) Dim fw As New BinaryWriter(stm) -- On 2009-06-10, Mike <unkn***@unknown.tv> wrote:
Show quoteHide quote > Tom Shelton wrote: I'm not sure I understand the problem:> >>> In other tools framework, they provides a FILE I/O class where you >>> override the the basic methods: >>> >>> Open >>> Read >>> Write >>> Seek >>> Lock >>> UnLock >>> EOF >>> Close >>> >>> etc. How can this be done in .NET? >>> >>> Thanks >> >> Inherit from System.IO.Stream. > > Nak and Tom: > > This is the approach I started, but I am still missing something, > namely the open file or file related overrides so I am checking that > out. Maybe that is what the CreateObjRef() is? > > I can do it by directly using our WcCreateFile() function and passing > the handle to the Stream class. Then all the other read, write, > positioning functions apply. > > In short, I have a fascimile of the WIN32 functions: > > HANDLE WcCreateFile(const char * fn, ....); > BOOL WcCloseHandle(HANDLE h); > BOOL WcWriteFile(WCHANDLE h, LPCVOID buffer, ....); > BOOL WcReadFile(WCHANDLE h, LPVOID buffer, DWORD requested, ...); > BOOL WcSetEndOfFile(WCHANDLE h); > DWORD WcSetFilePointer(WCHANDLE h, LONG dist, DWORD movemethod); > BOOL WcSetFileTime(WCHANDLE h, FILETIME &ft); > DWORD WcGetFileSize(WCHANDLE h); > BOOL WcGetFileTime(WCHANDLE h, FILETIME &ft); > > And the Read/Write, Pointer functions apply to the Stream class, but > the others need to be at higher level. That will allow for: > > dim stm as new stream = WCFile.Open("wc:\123.txt", fileMode.write) > Dim fw As New BinaryWriter(stm) > > -- A Stream in an abstract base class that represents a stream of bytes. Nothing more. If wrapping it in a BinaryWriter makes sense, then do that. I would say it would look something like: Dim fw As New BinaryWriter(New MyCustomFileStream ("wc:\123.txt", FileMode.Write) Your MyCustomFileStream class should handle all of the Seek, Open, Close, etc, etc methods. BinaryWriter will just call your implementations anyway. Maybe a little more information would give me a better perspective to give a better answer :) -- Tom Shelton Tom Shelton wrote:
> I'm not sure I understand the problem: Right. There are a few things here. It might up being as you suggest.> > A Stream in an abstract base class that represents a stream of bytes. Nothing > more. If wrapping it in a BinaryWriter makes sense, then do that. I would > say it would look something like: > > Dim fw As New BinaryWriter(New MyCustomFileStream ("wc:\123.txt", FileMode.Write) > > Your MyCustomFileStream class should handle all of the Seek, Open, Close, etc, > etc methods. BinaryWriter will just call your implementations anyway. > I got it to work adding the open to the stream class. I was looking to see if I can encapsulate the FILE class, looking at SafeHandle as this example: http://msdn.microsoft.com/en-us/library/microsoft.win32.safehandles.safefilehandle.aspx Streams use the safe handle provided by a derived stream class, i.e, FileStream, TextStream, SocketStream. Dim fs As New wioFileStream(filename, GENERIC_READ, 0, OPEN_EXISTING) Dim fs As New wioFileStream( _ new wioStream(filename), _ <-- returns safehandle GENERIC_READ, 0, OPEN_EXISTING) > Maybe a little more information would give me a better perspective Ideally, I would like to just inherit the FILE class so as I have now > to give a better answer :) in C/C++ wraps: HANDLE h = WcCreateFile() HANDLE h = CreateFile() The handle is the same, so that is all the streams need. -- |
|||||||||||||||||||||||