Example #1
0
        /// <summary>
        /// Attaches opened file.
        /// </summary>
        /// <param name="options">Options to use when attaching.</param>
        /// <exception cref="System.InvalidOperationException">The handle is invalid.</exception>
        /// <exception cref="System.NotSupportedException">Command is not supported in current state.</exception>
        /// <exception cref="System.UnauthorizedAccessException">A required privilege is not held by the client.</exception>
        /// <exception cref="System.ComponentModel.Win32Exception">Native error.</exception>
        /// <exception cref="System.IO.IOException">Access is denied.</exception>
        public void Attach(VirtualDiskAttachOptions options)
        {
            if (this.DiskType == VirtualDiskType.Iso) { options |= VirtualDiskAttachOptions.ReadOnly; }

            var parameters = new NativeMethods.ATTACH_VIRTUAL_DISK_PARAMETERS();
            parameters.Version = NativeMethods.ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1;

            int res = NativeMethods.AttachVirtualDisk(_handle, IntPtr.Zero, (NativeMethods.ATTACH_VIRTUAL_DISK_FLAG)options, 0, ref parameters, IntPtr.Zero);
            if (res == NativeMethods.ERROR_SUCCESS) {
            } else if (res == NativeMethods.ERROR_ACCESS_DENIED) {
                throw new IOException("Access is denied.");
            } else if (res == NativeMethods.ERROR_INVALID_HANDLE) {
                throw new InvalidOperationException("The handle is invalid.");
            } else if (res == NativeMethods.ERROR_BAD_COMMAND) {
                throw new System.NotSupportedException("Command is not supported in current state.");
            } else if (res == NativeMethods.ERROR_PRIVILEGE_NOT_HELD) {
                throw new UnauthorizedAccessException("A required privilege is not held by the client.");
            } else {
                throw new Win32Exception(res);
            }
        }
Example #2
0
        /// <summary>
        /// Attaches opened file.
        /// </summary>
        /// <param name="options">Options to use when attaching.</param>
        public void Attach(VirtualDiskAttachOptions options)
        {
            var parameters = new NativeMethods.ATTACH_VIRTUAL_DISK_PARAMETERS();
            parameters.Version = NativeMethods.ATTACH_VIRTUAL_DISK_VERSION.ATTACH_VIRTUAL_DISK_VERSION_1;

            int res = NativeMethods.AttachVirtualDisk(_handle, IntPtr.Zero, (NativeMethods.ATTACH_VIRTUAL_DISK_FLAG)options, 0, ref parameters, IntPtr.Zero);
            if (res == NativeMethods.ERROR_SUCCESS)
            {
            }
            else if (res == NativeMethods.ERROR_ACCESS_DENIED)
            {
                throw new System.IO.IOException("Access is denied.");
            }
            else if (res == NativeMethods.ERROR_INVALID_HANDLE)
            {
                throw new InvalidOperationException("The handle is invalid.");
            }
            else if (res == NativeMethods.ERROR_PRIVILEGE_NOT_HELD)
            {
                throw new System.UnauthorizedAccessException("A required privilege is not held by the client.");
            }
            else
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Native error {0}.", res));
            }
        }