Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取可移动驱动器的分区表_C#_Pinvoke - Fatal编程技术网

C# 如何读取可移动驱动器的分区表

C# 如何读取可移动驱动器的分区表,c#,pinvoke,C#,Pinvoke,我需要读取U盘的分区表。我用的是PInvoke。我可以成功创建文件,但如何读取分区表 代码如下: namespace PInvokeWithForms { public partial class Form1 : Form { const uint GENERIC_READ = 0x80000000; const uint OPEN_EXISTING = 3; IntPtr handle; [DllImport("

我需要读取U盘的分区表。我用的是PInvoke。我可以成功创建文件,但如何读取分区表

代码如下:

namespace PInvokeWithForms
{
    public partial class Form1 : Form
    {
        const uint GENERIC_READ = 0x80000000;
        const uint OPEN_EXISTING = 3;
        IntPtr handle;

        [DllImport("kernel32", SetLastError = true)]
        static extern unsafe IntPtr CreateFile(
              string FileName,                    // file name
              uint DesiredAccess,                 // access mode
              uint ShareMode,                     // share mode
              uint SecurityAttributes,            // Security Attributes
              uint CreationDisposition,           // how to create
              uint FlagsAndAttributes,            // file attributes
              int hTemplateFile                   // handle to template file
              );

        [DllImport("kernel32", SetLastError = true)]
        static extern unsafe bool ReadFile(
              IntPtr hFile,                       // handle to file
              void* pBuffer,                      // data buffer
              int NumberOfBytesToRead,            // number of bytes to read
              int* pNumberOfBytesRead,            // number of bytes read
              int Overlapped                      // overlapped buffer
              );

        [DllImport("kernel32", SetLastError = true)]
        static extern unsafe bool CloseHandle(
              IntPtr hObject   // handle to object
              );

        public bool Open(string FileName)
        {
            // open the existing file for reading          
            handle = CreateFile(
                  FileName,
                  GENERIC_READ,
                  0,
                  0,
                  OPEN_EXISTING,
                  0,
                  0);

            if (handle != IntPtr.Zero)
                return true;
            else
                return false;
        }

        public unsafe int Read(byte[] buffer, int index, int count)
        {
            int n = 0;
            fixed (byte* p = buffer)
            {
                if (!ReadFile(handle, p + index, count, &n, 0))
                    return 0;
            }
            return n;
        }

        public bool Close()
        {
            // close file handle
            return CloseHandle(handle);
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string FileName = "\\\\.\\G:";
            byte[] buffer = new byte[128];

            if (this.Open(FileName))
            {
                label1.Text = "Starting...\n";
                // Assume that an ASCII file is being read
                ASCIIEncoding Encoding = new ASCIIEncoding();

                int bytesRead;
                do
                {
                    bytesRead = this.Read(buffer, 0, buffer.Length);
                    string content = Encoding.GetString(buffer, 0, bytesRead);
                    label1.Text += content;
                }
                while (bytesRead > 0);

                this.Close();

            }
            else
            {
                label1.Text = "Failed to open requested file";
            }
        }
    }
}

看一看DeviceIoControl方法我将把这个无耻的自我插件放在这里——它并不完全是您所需要的,但许多方法都是类似的: