public static void CreateVDisk(KeyValuePairList <string, string> parameters)
        {
            if (!VerifyParameters(parameters, "file", "size"))
            {
                Console.WriteLine();
                Console.WriteLine("Invalid parameter.");
                HelpCreate();
                return;
            }

            long sizeInBytes;

            if (parameters.ContainsKey("size"))
            {
                long requestedSizeInMB = Conversion.ToInt64(parameters.ValueOf("size"), 0);
                sizeInBytes = requestedSizeInMB * 1024 * 1024;
                if (requestedSizeInMB <= 0)
                {
                    Console.WriteLine("Invalid size (must be specified in MB).");
                    return;
                }
            }
            else
            {
                Console.WriteLine("The SIZE parameter must be specified.");
                return;
            }

            if (parameters.ContainsKey("file"))
            {
                string path = parameters.ValueOf("file");

                if (new FileInfo(path).Exists)
                {
                    Console.WriteLine("Error: file already exists.");
                    return;
                }

                try
                {
                    m_selectedDisk = VirtualHardDisk.Create(path, sizeInBytes);
                    Console.WriteLine("The virtual disk file was created successfully.");
                }
                catch (IOException)
                {
                    Console.WriteLine("Error: Could not write the virtual disk file.");
                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    Console.WriteLine("Error: Access Denied, Could not write the virtual disk file.");
                    return;
                }
            }
            else
            {
                Console.WriteLine("The FILE parameter was not specified.");
            }
        }
示例#2
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            string path = txtFilePath.Text;
            long   size = (long)numericDiskSize.Value * 1024 * 1024;

            if (path == String.Empty)
            {
                MessageBox.Show("Please choose file location", "Error");
                return;
            }
            m_isWorking = true;
            new Thread(delegate()
            {
                DiskImage diskImage;
                try
                {
                    diskImage = VirtualHardDisk.Create(path, size);
                }
                catch (IOException ex)
                {
                    this.Invoke((MethodInvoker) delegate()
                    {
                        MessageBox.Show("Failed to create the disk: " + ex.Message, "Error");
                        txtFilePath.Enabled     = true;
                        btnBrowse.Enabled       = true;
                        numericDiskSize.Enabled = true;
                        btnOK.Enabled           = true;
                        btnCancel.Enabled       = true;
                    });
                    m_isWorking = false;
                    return;
                }
                bool isLocked = diskImage.ExclusiveLock();
                if (!isLocked)
                {
                    this.Invoke((MethodInvoker) delegate()
                    {
                        MessageBox.Show("Cannot lock the disk image for exclusive access", "Error");
                        txtFilePath.Enabled     = true;
                        btnBrowse.Enabled       = true;
                        numericDiskSize.Enabled = true;
                        btnOK.Enabled           = true;
                        btnCancel.Enabled       = true;
                    });
                    m_isWorking = false;
                    return;
                }
                m_diskImage = diskImage;
                m_isWorking = false;
                this.Invoke((MethodInvoker) delegate()
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                });
            }).Start();
            txtFilePath.Enabled     = false;
            btnBrowse.Enabled       = false;
            numericDiskSize.Enabled = false;
            btnOK.Enabled           = false;
            btnCancel.Enabled       = false;
        }