Пример #1
0
        private bool WriteToDisk()
        {
            DriveAccess d = new DriveAccess();
            byte[] buffer;
            bool retval = true;
            long written = 0;
            int writtenMB = 0;
            try
            {
                d.Open(device.name);
            }
            catch (Win32Exception e)
            {
                MessageBox.Show("Could not open device.  Are you sure you're running this as an administrator?", "Error", MessageBoxButtons.OK);
                return false;
            }

            if (device.volume != "") // Not necessary for devices with an unknown filesystem
            {
                DriveAccess c = new DriveAccess();
                try
                {
                    c.Open(device.volume);
                    c.DeviceIO(DriveAccess.FSCTL_DISMOUNT_VOLUME);
                }
                catch (Win32Exception e)
                {
                    /* This gets thrown on XP for devices that do not have a valid filesystem.
                     * Let's just ignore it and hope nothing breaks.
                     */
                }
            }

            try
            {
                d.DeviceIO(DriveAccess.FSCTL_LOCK_VOLUME);
            }
            catch (Win32Exception e)
            {
                MessageBox.Show("Exception caught: " + e, "Error", MessageBoxButtons.OK);
                d.Close();
                return false;
            }

            FileStream fileIn = new FileStream(imageFile, FileMode.Open);
            try
            {
                int count;
                buffer = new byte[BLOCK_SIZE];
                while ((count = fileIn.Read(buffer, 0, BLOCK_SIZE)) > 0)
                {
                    if (copyWorker.CancellationPending)
                    {
                        retval = false;
                        break;
                    }

                    written += count;
                    writtenMB = (int) (written / 1048576);
                    copyWorker.ReportProgress(writtenMB);
                    d.Write(buffer, (uint)count);
                }
            }
            catch (Win32Exception e)
            {
                MessageBox.Show("Exception caught during write: " + e, "Error", MessageBoxButtons.OK);
                d.Close();
                return false;
            }
            finally
            {
                fileIn.Close();
            }

            try { d.DeviceIO(DriveAccess.FSCTL_UNLOCK_VOLUME); }
            catch (Win32Exception e) { MessageBox.Show("Could not unlock device: " + e, "Error", MessageBoxButtons.OK); }

            d.Close();
            return retval;
        }
Пример #2
0
        private bool WriteToDisk()
        {
            DriveAccess d = new DriveAccess();

            byte[] buffer;
            bool   retval    = true;
            long   written   = 0;
            int    writtenMB = 0;

            try
            {
                d.Open(device.name);
            }
            catch (Win32Exception e)
            {
                MessageBox.Show("Could not open device.  Are you sure you're running this as an administrator?", "Error", MessageBoxButtons.OK);
                return(false);
            }

            if (device.volume != "") // Not necessary for devices with an unknown filesystem
            {
                DriveAccess c = new DriveAccess();
                try
                {
                    c.Open(device.volume);
                    c.DeviceIO(DriveAccess.FSCTL_DISMOUNT_VOLUME);
                }
                catch (Win32Exception e)
                {
                    /* This gets thrown on XP for devices that do not have a valid filesystem.
                     * Let's just ignore it and hope nothing breaks.
                     */
                }
            }

            try
            {
                d.DeviceIO(DriveAccess.FSCTL_LOCK_VOLUME);
            }
            catch (Win32Exception e)
            {
                MessageBox.Show("Exception caught: " + e, "Error", MessageBoxButtons.OK);
                d.Close();
                return(false);
            }

            FileStream fileIn = new FileStream(imageFile, FileMode.Open);

            try
            {
                int count;
                buffer = new byte[BLOCK_SIZE];
                while ((count = fileIn.Read(buffer, 0, BLOCK_SIZE)) > 0)
                {
                    if (copyWorker.CancellationPending)
                    {
                        retval = false;
                        break;
                    }

                    written  += count;
                    writtenMB = (int)(written / 1048576);
                    copyWorker.ReportProgress(writtenMB);
                    d.Write(buffer, (uint)count);
                }
            }
            catch (Win32Exception e)
            {
                MessageBox.Show("Exception caught during write: " + e, "Error", MessageBoxButtons.OK);
                d.Close();
                return(false);
            }
            finally
            {
                fileIn.Close();
            }

            try { d.DeviceIO(DriveAccess.FSCTL_UNLOCK_VOLUME); }
            catch (Win32Exception e) { MessageBox.Show("Could not unlock device: " + e, "Error", MessageBoxButtons.OK); }

            d.Close();
            return(retval);
        }