如何使用c#访问磁盘卷

时间:2015-08-14 23:14:15

标签: .net disk

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(
    string FileName,
    FileAccess DesiredAccess,
    FileShare ShareMode,
    IntPtr SecurityAttributes,
    FileMode CreationDisposition,
    FileAttributes FlagsAndAttributes,
    IntPtr hTemplate
    );
    public static byte[] Main()
    {

        IntPtr ptr  = CreateFile(@"\\.\C:", 0, FileShare.ReadWrite, IntPtr.Zero, FileMode.OpenOrCreate, FileAttributes.Normal, IntPtr.Zero);
        SafeFileHandle handleValue = new SafeFileHandle(ptr, true);
        FileStream disk = new FileStream(handleValue, FileAccess.Read);          
        byte[] inBuffer = new byte[512];
        disk.Seek(0, SeekOrigin.Begin);
        for (int i = 0; i < 512; i++)
        {
            try
            {
                inBuffer[i] = (byte)disk.ReadByte();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        return inBuffer; 
    }

使用此代码我尝试从卷读取,但是我收到此异常 System.UnauthorizedAccessException:拒绝访问该路径。 我搜索并发现这是因为Windows 7阻止直接访问卷如何克服这个问题?

0 个答案:

没有答案