Home » VB.NetRSS

fileget/fileput is slow in vb.net

Hi,

I convert vb6 app to vb.net and use fileget and fileput to read and save fixth size data structure to file, which is index file to allow you know where to read and write with random access. But the speed is slow compared with vb6, google search find using system.io stream to replace fileget/fileput, i don't know how to use stream to save fixed size data structure to file, which is index file. Can anyone give sample code or any other solution?

Thanks in advance,

Martin

 

6 Answers Found

 

Answer 1

using (FileStream stream  = new FileStream("path", FileMode.Open, FileAccess.ReadWrite))
{
  stream.Seek(somePosition, SeekOrigin.Begin);
  stream.Write(someArray, 0, someArraySize);
}
 

Answer 2

sorry for code  format to hard read, I post here again:

I think this is c# code, I am using vb.  still not quite understand to save  fixed-size data  structure. Below is part of app  code using fileget/fileput. Could you spend time to read  below code to help on how to use system.io.stream to read and write fix-size data structure  to file in order to run faster? Thanks a lot!

'---2 data structures

Structure

 	MinIndexType <VBFixedString(20)>Public IndexKey As String

 	Dim index  As Short

 	Dim RecordNumber As Short

End Structure

 

Structure MinuteType 

 	<VBFixedString(2 )>Public Id As String

 	<VBFixedString(2 )>Public ReportNumber As String

 	<VBFixedString(8 )>Public Parameter As String

End Structure


' ---declare data structure variable and file  number const

Public Const IndexFile As Short = 1 

Public Const DataFile As Short = 2

Dim MinIndex As MinIndexType

Dim IndexRecord As MinIndexType

Dim MinRecord As MinuteType 

'--file open

FileOpen(IndexFile, (Directory & IndexFileName), OpenMode.Random, , , Len(MinIndex)) 

FileOpen(DataFile, (Directory & DataFileName), OpenMode.Random, , , Len(MinRecord))

 

' --write to index file file

FilePut(IndexFile, IndexRecord, IndexRecord.RecordNumber)

 

'--- read and write  to data file

FileGet(DataFile, MinRecord, IndexRecord.Index)

FilePut(DataFile, MinRecord, IndexRecord.Index

 

'---Close files.

FileClose(IndexFile)

FileClose(DataFile)
 

Answer 3

I thought your question was simply how to use a Stream object. Your question really is, "How do I serialize raw data  to a VB-style index/data record file, without the metadata added by BinarySerializer?". That's an entirely different question.

I looked at the code  for Microsoft.VisualBasic.FileSystem.FilePutXXX. Heh.. Yea, that's not very efficient. It wasn't efficient in VB6 either, but moving to the managed world it got a lot worse.

The only way I can think of that will be faster is to p/invoke the Win32 file  API, and do it without marshalling. It's not too terribly difficult, but I can't help you in VB. I should have stayed away when I saw "VB", but it seemed a different question.

 

Answer 4

Very begining,  try to see if system.IO is a solution, but google  search find  stream may not save  fix-size data  structure into file  with random  access, so I post code  here to let someone help find another solution.

also can you tell how to use win32file app  for this purpose. Thanks,

 

Answer 5

You'll do better with you own file  i/o for VB6 binary files.  Use filestream Read and Write and insert and extract your structure  from the byte().
 

Answer 6

Hi, John,

could you give  sample code  to help on how to convert  above code using your approach?

Thanks,

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter