我正在直接使用C#从磁盘读取并使用kernel32 ReadFile方法。我只想读取特定扇区以节省时间,但ReadFile从第一个扇区读取到N个扇区。如何选择只能读取自己的扇区?

    [StructLayout(LayoutKind.Sequential)]
    public struct OVERLAPPED
    {
        public uint Internal;
        public uint InternalHigh;
        public uint Offset;
        public uint OffsetHigh;
        public int hEvent;
    }

    [DllImport("kernel32", SetLastError = true)]
    static extern int CreateFile(string filename, uint desiredAccess, uint shareMode, IntPtr attributes, uint creationDisposition, uint flagsAndAttributes, IntPtr templateFile);

    [DllImport("kernel32", SetLastError = true)]
    public static extern Boolean CloseHandle(int handle);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern Boolean ReadFile(IntPtr hFile, Byte[] buffer, UInt32 BytesToRead, ref UInt32 BytedRead, OVERLAPPED OverLapped);
    static int EIGHT_K = 8192;
    static int FIVE_TWELVE_BYTES = 512;
    static uint GENERIC_READ = 0x80000000;
    static uint OPEN_EXISTING = 3;
    static uint FILE_SHARE_READ = 1;
    static uint FILE_SHARE_WRITE = 2;

    [STAThread]
    private void button1_Click(object sender, EventArgs e)
    {
        int fileHandle = 0;
        bool returnVal = true;

        try
        {
            // Open the device specified (Using the boot partition)
            string deviceName = @"\\.\f:";
            fileHandle = CreateFile(deviceName, GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE, (IntPtr)0, OPEN_EXISTING, 0,(IntPtr)0);
            if (fileHandle != -1)
            {
                Byte[] sector = new Byte[EIGHT_K];
                UInt32 bytesRead = (uint)EIGHT_K;
                OVERLAPPED ol = new OVERLAPPED();

                // Can't get a FileStream ctor to work so I am using Win32 API ReadFile
                bool worked = ReadFile((IntPtr)fileHandle, sector, (uint)EIGHT_K, ref bytesRead, ol);
                return;

            }
        }

        catch (Exception ex)
        {
            return;
        }
        finally
        {
            CloseHandle(fileHandle);
        }
        return;
    }


我要标记DVD,直到需要原始DVD才能运行该程序。

最佳答案

您的OVERLAPPED结构声明不正确,并且在64位进程中不正确。但是无论如何您都不需要它。您没有执行重叠的I / O。这也是因为ReadFile的声明不正确。该函数需要一个指向OVERLAPPED结构的指针。您通过值传递它。

无论如何,您只需要考虑重叠的I / O。因此,通过从代码中删除OVERLAPPED结构声明来解决此问题。并这样声明ReadFile

[DllImport("kernel32.dll", SetLastError = true)]
public static extern Boolean ReadFile(IntPtr hFile, Byte[] buffer,
    UInt32 BytesToRead, out UInt32 BytedRead, IntPtr Overlapped);


传递IntPtr.Zero作为Overlapped参数。并确保您检查ReadFile的返回值是否有错误。

下一步是寻找文件中的位置。为此使用SetFilePointerEx

DllImport("kernel32.dll")]
static extern bool SetFilePointerEx(IntPtr hFile, long liDistanceToMove,
   out long lpNewFilePointer, uint dwMoveMethod);


请查阅SetFilePointerEx的文档,以了解如何调用此函数。

由于您使用的是直接磁盘访问,因此您当然需要将读取与扇区边界对齐。

关于c# - 用C#读取Pinvoke的DVD的特定扇区,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24746540/

10-12 12:37
查看更多