示例#1
0
        private string GetDiskId(int ordinal)
        {
            VirtualDisk disk = _disks[ordinal];

            if (disk.IsPartitioned)
            {
                Guid guid = disk.Partitions.DiskGuid;
                if (guid != Guid.Empty)
                {
                    return("DG" + guid.ToString("B"));
                }
            }

            int sig = disk.Signature;

            if (sig != 0)
            {
                return("DS" + sig.ToString("X8", CultureInfo.InvariantCulture));
            }

            return("DO" + ordinal);
        }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the VolumeManager class.
 /// </summary>
 /// <param name="initialDisk">The initial disk to add.</param>
 public VolumeManager(VirtualDisk initialDisk)
     : this()
 {
     AddDisk(initialDisk);
 }
示例#3
0
 /// <summary>
 /// Gets the physical volumes held on a disk.
 /// </summary>
 /// <param name="disk">The disk to inspect.</param>
 /// <returns>An array of volumes.</returns>
 /// <remarks>If the disk isn't partitioned, this method returns the entire disk contents
 /// as a single volume.</remarks>
 public static PhysicalVolumeInfo[] GetPhysicalVolumes(VirtualDisk disk)
 {
     return(new VolumeManager(disk).GetPhysicalVolumes());
 }
示例#4
0
        /// <summary>
        /// Opens an existing virtual disk.
        /// </summary>
        /// <param name="path">The path of the virtual disk to open, can be a URI.</param>
        /// <param name="forceType">Force the detected disk type (<c>null</c> to detect).</param>
        /// <param name="access">The desired access to the disk.</param>
        /// <param name="user">The user name to use for authentication (if necessary).</param>
        /// <param name="password">The password to use for authentication (if necessary).</param>
        /// <returns>The Virtual Disk, or <c>null</c> if an unknown disk format.</returns>
        /// <remarks>
        /// The detected disk type can be forced by specifying a known disk type:
        /// RAW, VHD, VMDK, etc.
        /// </remarks>
        public static VirtualDisk OpenDisk(string path, string forceType, FileAccess access, string user, string password)
        {
            Uri         uri    = PathToUri(path);
            VirtualDisk result = null;

            Type transportType;

            if (!DiskTransports.TryGetValue(uri.Scheme.ToUpperInvariant(), out transportType))
            {
                throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Unable to parse path '{0}'", path), path);
            }

            VirtualDiskTransport transport = (VirtualDiskTransport)Activator.CreateInstance(transportType);

            try
            {
                transport.Connect(uri, user, password);

                if (transport.IsRawDisk)
                {
                    result = transport.OpenDisk(access);
                }
                else
                {
                    bool foundFactory;
                    VirtualDiskFactory factory;

                    if (!string.IsNullOrEmpty(forceType))
                    {
                        foundFactory = TypeMap.TryGetValue(forceType, out factory);
                    }
                    else
                    {
                        string extension = Path.GetExtension(uri.AbsolutePath).ToUpperInvariant();
                        if (extension.StartsWith(".", StringComparison.Ordinal))
                        {
                            extension = extension.Substring(1);
                        }

                        foundFactory = ExtensionMap.TryGetValue(extension, out factory);
                    }

                    if (foundFactory)
                    {
                        result = factory.OpenDisk(transport.GetFileLocator(), transport.GetFileName(), access);
                    }
                }

                if (result != null)
                {
                    result._transport = transport;
                    transport         = null;
                }

                return(result);
            }
            finally
            {
                if (transport != null)
                {
                    transport.Dispose();
                }
            }
        }