Ejemplo n.º 1
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; });
            });
        }
Ejemplo n.º 2
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);
            });
        }
Ejemplo n.º 3
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);
            });
        }
Ejemplo n.º 4
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);
            });
        }
Ejemplo n.º 5
0
        public MainForm()
        {
            InitializeComponent();

            checkBoxUseMBR.Checked = true;

            MessageBoxEx.Owner = this.Handle;

            toolStripStatusLabel1.Text = @"Initialised. Licensed under GPLv3. Use at own risk!";

            saveFileDialog1.OverwritePrompt = false;
            saveFileDialog1.Filter          = @"Image Files (*.img,*.bin,*.sdcard)|*.img;*.bin;*.sdcard|Compressed Files (*.zip,*.gz,*tgz)|*.zip;*.gz;*.tgz|All files (*.*)|*.*";

            // Set version into title
            var version = Assembly.GetEntryAssembly().GetName().Version;

            Text += @" v" + version;

            // Set app icon (not working on Mono/Linux)
            if (Environment.OSVersion.Platform != PlatformID.Unix)
            {
                Icon = Utility.GetAppIcon();
            }

            PopulateDrives();
            if (comboBoxDrives.Items.Count > 0)
            {
                EnableButtons();
            }
            else
            {
                DisableButtons(false);
            }

            // Read registry values
            var key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Dynamic Devices Ltd\\DiskImager");

            if (key != null)
            {
                var file = (string)key.GetValue("FileName", "");
                if (File.Exists(file))
                {
                    textBoxFileName.Text = file;
                }

                var drive = (string)key.GetValue("Drive", "");
                if (string.IsNullOrEmpty(drive))
                {
                    foreach (var cbDrive in comboBoxDrives.Items)
                    {
                        if ((string)cbDrive == drive)
                        {
                            comboBoxDrives.SelectedItem = cbDrive;
                        }
                    }
                }

                Globals.CompressionLevel = (int)key.GetValue("CompressionLevel", Globals.CompressionLevel);
                Globals.MaxBufferSize    = (int)key.GetValue("MaxBufferSize", Globals.MaxBufferSize);

                key.Close();
            }

            // Create disk object for media accesses
            var pid = Environment.OSVersion.Platform;

            if (pid == PlatformID.Unix)
            {
                _diskAccess = new LinuxDiskAccess();
            }
            else
            {
                _diskAccess = new Win32DiskAccess();
            }

            _disk = new Disk(_diskAccess);

            _disk.OnLogMsg   += _disk_OnLogMsg;
            _disk.OnProgress += _disk_OnProgress;

            // Detect insertions / removals
            _watcher.DeviceArrived += OnDriveArrived;
            _watcher.DeviceRemoved += OnDriveRemoved;
            StartListenForChanges();
        }