示例#1
0
        /// <summary>
        /// Write to removable media from file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonWriteClick(object sender, EventArgs e)
        {
            if (checkedListBoxDrives.CheckedItems.Count == 0)
                return;

            var drives = checkedListBoxDrives.CheckedItems.Cast<Object>().Select(d => d.ToString()).ToArray();

            if (drives.Any(d => d.ToUpper().StartsWith("C:")))
            {
                var dr =
                    MessageBox.Show(
                        Resources.MainForm_ButtonWriteClick_C__is_almost_certainly_your_main_HDD,
                        Resources.MainForm_ButtonReadClick_____WARNING____, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr != DialogResult.Yes)
                    return;
            }

            ClearLayoutPanels();
            if (ChooseFileToWrite() == false)
                return;

            var filePath = lastDirectoryUsed + "//" + lastFileUsed;

            if (!File.Exists(filePath))
            {
                MessageBox.Show(Resources.MainForm_ButtonWriteClick_File_does_not_exist_, Resources.MainForm_ButtonWriteClick_I_O_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DisableButtons(true);

            Task.Factory.StartNew(() =>
            {
                DiskAccesses.Clear();
                _disks.Clear();

                var tasks = drives.Select(drive => Task.Factory.StartNew(() =>
                {
                    var diskAccess = NewDiskAccess();
                    var disk = new Disk(diskAccess);

                    Thread.CurrentThread.CurrentUICulture = CurrentLocale;
                    SendProgressToUI(disk);

                    DiskAccesses.Add(diskAccess);
                    _disks.Add(disk);

                    var res = false;
                    try
                    {
                        res = disk.WriteDrive(drive, filePath, _eCompType, unmountDrivesToolStripMenuItem.Checked);
                    }
                    catch (Exception ex)
                    {
                        Invoke(new Action( () => MessageBox.Show(ex.Message, @"Exception at WriteDrive", MessageBoxButtons.OK, MessageBoxIcon.Error)));
                    }

                    if (!res && !disk.IsCancelling)
                    {
                        Invoke(new Action ( () => MessageBox.Show(Resources.MainForm_ButtonWriteClick_Problem_writing_to_disk__Is_it_write_protected_, Resources.MainForm_ButtonWriteClick_Write_Error,
                            MessageBoxButtons.OK, MessageBoxIcon.Error)));
                    }

                })).ToArray();

                Task.WaitAll(tasks);
                Invoke((MethodInvoker)EndInfo);
                Invoke((MethodInvoker)EnableButtons);
            });
        }
示例#2
0
        /// <summary>
        /// Before writing / reading we should check that FileName is not empty
        /// </summary>
        //private bool GetPathIfEmpty()
        //{
        //    return !string.IsNullOrEmpty(textBoxFileName.Text) || ChooseFile();
        //}
        /// <summary>
        /// Shows on-going process in UI using created elements
        /// </summary>
        private void SendProgressToUI(Disk disk)
        {
            Invoke((MethodInvoker)delegate
            {
                var progressBar = new ProgressBar { Size = new Size(flowLayoutPanelProgressBars.Width - 10, 10) };
                var label = new Label { Size = new Size(flowLayoutPanelProgressLabels.Width - 10, 17) };
                flowLayoutPanelProgressBars.Controls.Add(progressBar);
                flowLayoutPanelProgressLabels.Controls.Add(label);

                disk.OnLogMsg += (o, message) => Invoke((MethodInvoker) delegate { label.Text = message; });
                disk.OnProgress +=
                    (o, progressPercentage) => Invoke((MethodInvoker) delegate { progressBar.Value = progressPercentage; });
            });
        }
示例#3
0
        /// <summary>
        /// Select a file for read/write from/to removable media
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void ButtonChooseFileClick(object sender, EventArgs e)
        //{
        //    ChooseFile();
        //}
        /// <summary>
        /// Read from removable media to file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonReadClick(object sender, EventArgs e)
        {
            if (checkedListBoxDrives.CheckedItems.Count != 1)
            {
                MessageBox.Show(
                    Resources.MainForm_ButtonReadClick_You_can_read_from_only_one_drive_at_a_time,
                    Resources.MainForm_ButtonReadClick_____WARNING____, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var drive = (string)checkedListBoxDrives.CheckedItems[0];

            ClearLayoutPanels();
            if (ChooseFileToRead() == false)
                return;

            var filePath = lastDirectoryUsed + "//" + lastFileUsed;

            DisableButtons(true);

            Task.Factory.StartNew(() =>
            {
                DiskAccesses.Clear();
                _disks.Clear();

                var diskAccess = NewDiskAccess();
                var disk = new Disk(diskAccess);

                Thread.CurrentThread.CurrentUICulture = CurrentLocale;
                SendProgressToUI(disk);

                DiskAccesses.Add(diskAccess);
                _disks.Add(disk);

                var res = false;
                try
                {
                    res = disk.ReadDrive(drive, filePath, _eCompType, unmountDrivesToolStripMenuItem.Checked);
                }
                catch (Exception ex)
                {
                    Invoke(new Action(() => MessageBox.Show(ex.Message, @"Exception at ReadDrive", MessageBoxButtons.OK, MessageBoxIcon.Error)));
                }

                if (!res && !disk.IsCancelling)
                {
                    Invoke(new Action ( () => MessageBox.Show(Resources.MainForm_ButtonReadClick_Problem_with_reading_from_disk_, Resources.MainForm_ButtonReadClick_Read_Error,
                        MessageBoxButtons.OK, MessageBoxIcon.Error)));
                }

                Invoke((MethodInvoker) EnableButtons);
                if (res)
                    Invoke((MethodInvoker) EndInfo);
            });
        }