如何阅读行业并在VB.NET中写入另一个行业 [英] HOW TO READ A SECTOR AND WRITE TO ANOTHER SECTOR IN VB.NET
本文介绍了如何阅读行业并在VB.NET中写入另一个行业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我想知道我们如何逐个阅读或逐个部门写作..
>
这可以用c c ++或Python编程。
但有没有办法在vb.net中编写这个任务。
i需要你的帮助。
请提供反馈。
关注
Hello ,
I want to know how we can read sector by sector or write sector by sector..
this can be programmed in c c++ or Python .
but is there any way to code this task in vb.net.
i need your help.
plz give your feedback.
Regards
推荐答案
要访问硬盘驱动器的扇区级别,您需要使用低级API。
基本上,这里你需要的两个函数是这样的:
To access a hard drive''s sector level, you need to use low level API.
Basically, the two functions you will need here will be something like that:
Private Function ReadHDBytes (ByVal ByteCount As Long, ByRef DataBytes () As Byte, ByRef ActuallyReadByte As Long) As Boolean
Dim RetVal As Long
RetVal = ReadFile (hDisk, DataBytes (0), ByteCount, ActuallyReadByte, 0)
ReadHDBytes = Not (RetVal = 0)
End Function
Private Function WriteHDBytes (ByVal ByteCount As Long, ByRef DataBytes () As Byte) As Boolean
Dim RetVal As Long
Dim BytesToWrite As Long
Dim BytesWritten As Long
RetVal = WriteFile (hDisk, DataBytes (0), ByteCount, BytesWritten, 0)
WriteHDBytes = Not (RetVal = 0)
End Function
之前能够调用这些函数,您必须添加以下代码行,以便从VB访问Win32 API />
Before being able to call these functions, you must add the following lines of code in order to access Win32 API from VB.
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long '/ / declare has changed
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long '/ / declare has changed
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Const GENERIC_READ = & H80000000
Private Const GENERIC_WRITE = & H40000000
Private Const FILE_SHARE_READ = & H1
Private Const FILE_SHARE_WRITE = & H2
Private Const OPEN_EXISTING = 3
Private Const INVALID_HANDLE_VALUE = -1
'/ / file seek
Private Const FILE_BEGIN = 0
Private Const FILE_CURRENT = 1
Private Const FILE_END = 2
Private Const ERROR_SUCCESS = 0 &
'/ / device io control
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Private Const IOCTL_DISK_GET_DRIVE_GEOMETRY As Long = & H70000'458752
Private Const IOCTL_STORAGE_GET_MEDIA_TYPES_EX As Long = & H2D0C04
Private Const IOCTL_DISK_FORMAT_TRACKS As Long = & H7C018
Private Const FSCTL_LOCK_VOLUME As Long = & H90018
Private Const FSCTL_UNLOCK_VOLUME As Long = & H9001C
Private Const FSCTL_DISMOUNT_VOLUME As Long = & H90020
Private Const FSCTL_GET_VOLUME_BITMAP = & H9006F
'/ / type
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Enum MEDIA_TYPE
Unknown
F5_1Pt2_512
F3_1Pt44_512
F3_2Pt88_512
F3_20Pt8_512
F3_720_512
F5_360_512
F5_320_512
F5_320_1024
F5_180_512
F5_160_512
RemovableMedia
FixedMedia
End Enum
Private Type DISK_GEOMETRY
Cylinders As LARGE_INTEGER
MediaType As MEDIA_TYPE
TracksPerCylinder As Long
SectorsPerTrack As Long
BytesPerSector As Long
End Type
'/ / private vars
Private hDisk As Long 'disk handle
Private lpGeometry As DISK_GEOMETRY 'disk info
Private lBufferSize As Long 'the buffer size of read / write
当然,这不是一个完整的解决方案,只是为了给你一些指示从这里继续。
Of course, this is not a complete solution, but only to give you some directions to continue from here.
这篇关于如何阅读行业并在VB.NET中写入另一个行业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文