Example #1
0
        public bool ReadVhd(string filename)
        {
            try
            {
                if (CSharp.cc.Files.DoubleCheckIfFileIsBeingUsed(filename))
                {
                    List <String> whoLockedIt = FileLockInfo.Handle(filename);
                    string        whoString   = String.Empty;
                    foreach (var who in whoLockedIt)
                    {
                        whoString += who.ToString() + ", ";
                    }
                    MessageBox.Show(filename + " Locked by " + whoString);
                    return(false);
                }
                vhd = new VirtualHardDisk(filename);
                if (vhd.vhdReadException == null)
                {
                    if (!vhd.DiskType.Equals("Fixed"))
                    {
                        ShowUsage();
                    }
                    ShowDetailedUsage();
                }
            }
            //catch (VhdReadException ex)
            //{
            //    vhd.vhdReadException = ex;
            //}
            catch (Exception ex)
            {
                MessageBox.Show(filename + ": " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                this.AddDisk(vhd);
            }


            return(true);
        }
Example #2
0
        private void attachToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this._disk == null)
            {
                if (CSharp.cc.Files.CheckIfFileIsBeingUsed(vhd.footer.FileName))
                {
                    List <String> whoLockedIt = FileLockInfo.Handle(vhd.footer.FileName);
                    string        whoString   = String.Empty;
                    foreach (var who in whoLockedIt)
                    {
                        whoString += who.ToString() + ", ";
                    }
                    MessageBox.Show(vhd.footer.FileName + " Locked by " + whoString);
                    return;
                }
                this._disk = new Medo.IO.VirtualDisk(vhd.footer.FileName);
                try
                {
                    this._disk.Open();
                }
                catch (InvalidOperationException ex)
                {
                    // "Native error {0}."
                    string message     = ex.Message;
                    int    nativeError = 0;
                    if (message.StartsWith("Native error"))
                    {
                        nativeError = Convert.ToInt32(message.Replace("Native error ", "").Replace(".", ""));
                    }

                    if (nativeError == 0x20) // ERROR_SHARING_VIOLATION
                    {
                        this._disk = null;
                        List <String> whoLockedIt = FileLockInfo.Handle(vhd.footer.FileName);
                        string        whoString   = String.Empty;
                        foreach (var who in whoLockedIt)
                        {
                            whoString += who.ToString() + ", ";
                        }
                        MessageBox.Show("Locked by " + whoString);
                        // FileLockInfo.DetectOpenFiles.GetOpenFilesEnumerator(0);

                        return;
                        //this._disk = new Medo.IO.VirtualDisk(vhd.footer.FileName);
                        //this._disk.Open(Medo.IO.VirtualDisk.NativeMethods.VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_ATTACH_RO);
                        //// this._disk.Attach(Medo.IO.VirtualDiskAttachOptions.None);
                        //this._disk.Attach(Medo.IO.VirtualDiskAttachOptions.ReadOnly);
                        //// this._disk.Detach();
                        //MessageBox.Show(CSharp.cc.WinApi.SystemErrorCodes.GetMessage(nativeError), "Already attached, read only now.");
                        //// return;
                    }
                    else
                    {
                        MessageBox.Show(CSharp.cc.WinApi.SystemErrorCodes.GetMessage(nativeError), "Failed to Open VHD");
                        return;
                    }
                }
                catch (System.IO.InvalidDataException ex) {
                    MessageBox.Show(ex.Message + " at " + ex.TargetSite.ToString(), "Error attaching VHD");
                }
                catch (System.IO.FileNotFoundException ex) {
                    new Error("File not found", ex);
                }

                /*
                 *  if ((res == NativeMethods.ERROR_FILE_NOT_FOUND) || (res == NativeMethods.ERROR_PATH_NOT_FOUND)) {
                 * throw new System.IO.FileNotFoundException("File not found.");
                 * } else if (res == NativeMethods.ERROR_FILE_CORRUPT) {
                 * throw new System.IO.InvalidDataException("File format not recognized.");
                 * } else {
                 * throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", res));
                 * }
                 * }*/

                if (this._disk == null)
                {
                    return;
                }
                if (!this._disk.IsOpen)
                {
                    return;
                }

                try
                {
                    if (!this._disk.IsAttached)
                    {
                        this._disk.Attach(Medo.IO.VirtualDiskAttachOptions.None);
                    }
                }
                catch (InvalidOperationException ex)
                {
                    // "Native error {0}."
                    string message     = ex.Message;
                    int    nativeError = 0;
                    if (message.StartsWith("Native error"))
                    {
                        nativeError = Convert.ToInt32(message.Replace("Native error ", "").Replace(".", ""));
                    }

                    MessageBox.Show(CSharp.cc.WinApi.SystemErrorCodes.GetMessage(nativeError), "Failed to Attach VHD");
                    return;
                }
                MessageBox.Show("VHD Mounted as " + this._disk.GetPhysicalPath());

                SetAttached(this._disk.GetPhysicalPath());
            }


            // (sender as System.Windows.Forms.ToolStripDropDownItem).GetCurrentParent;
        }