public class readdriver
    {
        public const short FILE_ATTRIBUTE_NORMAL = 0x80;
        public const short INVALID_HANDLE_VALUE = -1;
        public const uint GENERIC_READ = 0x80000000;
        public const uint GENERIC_WRITE = 0x40000000;
        public const uint FILE_SHARE_READ = 0x00000001;
        public const uint CREATE_NEW = 1;
        public const uint CREATE_ALWAYS = 2;
        public const uint OPEN_EXISTING = 3;
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,uint dwFlagsAndAttributes,IntPtr hTemplateFile);
        private SafeFileHandle handleValue = null;
        public readdriver(string driverName)
        {
            Load(driverName);
        }
         public void Load(string driverName)
        {
           if (driverName == null && driverName.Length == 0)
            {
                throw new ArgumentNullException("driverName");
            }            handleValue = CreateFile("\\\\.\\"+driverName , GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
            if (handleValue.IsInvalid)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
            }
        }
        public SafeFileHandle Handle
        {
            get
            {
                if (!handleValue.IsInvalid)
                {
                    return handleValue;
                }
                else
                {
                    return null;
                }
            }
        }
    }   
}
为什么传入光驱盘符的时候能用,但是传入的是硬盘盘符的时候却不能执行成功呢?请各位大大帮忙出出注意好吗?这是我程序的最后一部分了,谢谢各位先

解决方案 »

  1.   


    handleValue = CreateFile("\\\\.\\"+driverName , GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);//改为
    handleValue = CreateFile(@"\\.\"+driverName , GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);  
      

  2.   

    handleValue = CreateFile(@"\\?\"+driverName , GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);  
      

  3.   

    为什么我在使用deviceiocontrol的时候,不能使用这样的handle呢?