本文介绍了帮助SetFilePointer vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想知道我是否可以得到一些指示,双关语意图:-)



我把一些代码放在一起创建一个文件来自驱动器号,然后尝试以512字节块读取它。



然后,我想查看缓冲区的前三个字节,并将它们与jpeg标头进行比较。如果找到一个jpeg标题,增加一个计数器,更新我的表单上的标签,然后使用SetFilePointer将读取前进512字节。



我的代码是如下:



Ok, so wonder if I can get a few pointers on this, pun intended :-)

I have thrown some code together which creates a file from a drive letter and then attempts to read it in 512 byte blocks.

I then want to look at the first three bytes of the buffer and compare them to a jpeg header. If a jpeg header is found, increase a counter, update a label on my form and then advance the read by 512 bytes using SetFilePointer.

The code I have is as follows:

Dim Hnd As Integer
       Dim nBytesToRead As Integer
       'Dim bResult As Integer
       Dim Buffer(512) As Byte

       Dim lowbyte As Integer       ' low dword of file pointer position
       Dim highbyte As Integer      ' high dword of file pointer position

       Hnd = CreateFile("\\.\" & Mid(strDrive, 1, 2), GENERIC_READ Or GENERIC_WRITE,      FILE_SHARE_READ Or FILE_SHARE_WRITE, _
       Nothing, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, IntPtr.Zero)

       Dim loopcount As Integer = 0 ' to keep track of how many 512 blocks are being read
       Dim count As Integer = 0 ' count the number of files found

       If (Hnd <> INVALID_HANDLE_VALUE) Then

           While True

               ReadFile(Hnd, Buffer, 512, nBytesToRead, New System.Threading.NativeOverlapped)

               ' find magic number
                   If buffer(0)= 255 and buffer(1) = 216 and buffer (2) = 255 Then
                   ' means we have the start of a new file

                       ' if already have a file open, close it
                       ' TODO

                       ' increase the file count
                       count += 1
                       Form1.lbl_RecoveredCount.Text = count + 1 ' +1 because its base-zero
                       Application.DoEvents()
                   End If

               'advance to the next 512 block
               loopcount += 1
               lowbyte = 512 * loopcount
               highbyte = 0
               SetFilePointer(Hnd, lowbyte, highbyte, FILE_BEGIN)

               Array.Clear(Buffer, Buffer.GetLowerBound(0), Buffer.Length)

           End While
           CloseHandle(Hnd)
       Else
           Return False
       End If

       Return True

   End Function





此时,SetFilePointer我认为没有进步,或者如果确实如此,Buffer不会更新TE。谁能看到我错过的东西?



非常感谢。



p.s.我知道我还没有处理潜在的异常,当代码工作时我会覆盖这个(希望如此)。



At the minute, the SetFilePointer I dont think advances, or if it does, Buffer does not update. Can anyone see what I''m missing?

Much appreciated.

p.s. I''m aware I havent handled potential exceptions yet, I''ll cover this (hopefully) when the code is working.

推荐答案


这篇关于帮助SetFilePointer vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:04