Beispiel #1
0
        public LocalStorageDevice(string filename, bool enablePrivileges = false,
                                  bool useIoCompletionPort = false, bool unbuffered = false, bool deleteOnClose = false)
        {
            this.filename            = filename;
            this.enablePrivileges    = enablePrivileges;
            this.useIoCompletionPort = useIoCompletionPort;

            if (enablePrivileges)
            {
                Native32.EnableProcessPrivileges();
            }

            Native32.GetDiskFreeSpace(filename.Substring(0, 3),
                                      out lpSectorsPerCluster,
                                      out lpBytesPerSector,
                                      out lpNumberOfFreeClusters,
                                      out lpTotalNumberOfClusters);

            uint fileAccess = Native32.GENERIC_READ | Native32.GENERIC_WRITE;
            //            uint fileShare = unchecked(((uint)FileShare.ReadWrite & ~(uint)FileShare.Inheritable));
            uint fileShare    = unchecked (((uint)FileShare.Read & ~(uint)FileShare.Inheritable));
            uint fileCreation = unchecked ((uint)FileMode.OpenOrCreate);
            uint fileFlags    = Native32.FILE_FLAG_OVERLAPPED;

            if (unbuffered)
            {
                fileFlags = fileFlags | Native32.FILE_FLAG_NO_BUFFERING;
            }

            if (deleteOnClose)
            {
                fileFlags = fileFlags | Native32.FILE_FLAG_DELETE_ON_CLOSE;
            }

            logHandle = Native32.CreateFileW(filename,
                                             fileAccess,
                                             fileShare,
                                             IntPtr.Zero,
                                             fileCreation,
                                             fileFlags,
                                             IntPtr.Zero);

            if (enablePrivileges)
            {
                Native32.EnableVolumePrivileges(filename, logHandle);
            }

            if (useIoCompletionPort)
            {
                ioCompletionPort = Native32.CreateIoCompletionPort(
                    logHandle,
                    IntPtr.Zero,
                    (uint)logHandle.DangerousGetHandle().ToInt64(),
                    0);
            }
            ThreadPool.BindHandle(logHandle);
        }