How to read files from a Differencing Disk

Jun 29, 2009 at 7:21 PM

Hi,

I'd like to be able to interate through the files and directorys on a differencing VHD. I've written the following code, which works on the parent. However if I select a child image I get an error message saying that a differencing disk cannot be opened with a stream. Is there another way to do this?

One other very quick question, whilst i'm here; how do I get the drive letter for each partition?

 

        private void ReadVHD()
        {
            using (VirtualDisk disk = OpenDisk(txtScanVHD.Text))
            {
                using (Stream partitionStream = disk.Partitions[0].Open())
                {
                    DiscUtils.Ntfs.NtfsFileSystem ntfs = new DiscUtils.Ntfs.NtfsFileSystem(partitionStream);
                    ntfs.NtfsOptions.HideHiddenFiles = true;
                    ntfs.NtfsOptions.HideSystemFiles = true;
                    ntfs.NtfsOptions.HideMetafiles = true;

                    DiscDirectoryInfo dirRoot = ntfs.Root;

                    WalkThroughDirs(dirRoot);
                }
            }
        }


        private void WalkThroughDirs(DiscDirectoryInfo dirInfo)
        {
            DiscFileInfo[] fileArray = dirInfo.GetFiles();
            foreach (DiscFileInfo fileInfo in fileArray)
            {
                textInfo.AppendText(dirInfo.Name + "\\" + fileInfo.Name + "\r\n");
            }

            DiscDirectoryInfo[] dirArray = dirInfo.GetDirectories();
            foreach (DiscDirectoryInfo subDirInfo in dirArray)
            {
                textInfo.AppendText(subDirInfo.Name + "\r\n");
                WalkThroughDirs(subDirInfo);
            }
        }

        private VirtualDisk OpenDisk(string path)
        {
            if (path.EndsWith(".VHD", StringComparison.OrdinalIgnoreCase))
            {
                return new DiscUtils.Vhd.Disk(new FileStream(path, FileMode.Open, FileAccess.Read), Ownership.Dispose);
            }
            throw new NotSupportedException("Unrecognised file extension");
        }


Coordinator
Jun 29, 2009 at 7:44 PM
Edited Jun 29, 2009 at 7:44 PM

Hi,

There are two options - the easiest way - in OpenDisk, do this:

return new DiscUtils.Vhd.Disk(path)

DiscUtils will do it's best to find the parent file by looking at the relative and absolute paths held in the disk you provide.

Alternatively, you can use the new constructor that lets you pass in multiple DiskImageFile instances:

List<DiskImageFile> files = new List<DiskImageFile>();

string lastPath = @"C:\temp\child2.vhd";
DiskImageFile firstFile = new DiskImageFile(new FileStream(lastPath, FileMode.Open, FileAccess.Read));
files.Add(firstFile);
while (files[files.Count - 1].NeedsParent)
{
    DiskImageFileInfo info = files[files.Count - 1].Information;

    foreach (string path in info.DynamicParentLocators)
    {
        Console.WriteLine("Path: " + path);

        // ************************************
        // Do own path processing logic here...
        string processedPath = Path.Combine(Path.GetDirectoryName(lastPath), path);
        // ************************************

        Console.WriteLine("Path: " + path + ", resolved to: " + processedPath);

        if (File.Exists(processedPath))
        {
            DiskImageFile newFile = new DiskImageFile(new FileStream(processedPath, FileMode.Open, FileAccess.Read));
            files.Add(newFile);
            break;
        }
    }
}

Disk disk = new Disk(files.ToArray(), Ownership.Dispose)

This is more suitable if the built-in logic to find the parent disk doesn't meet your needs (perhaps you need to prompt the user to indicate the parent disk location, if it can't be found automatically).  To use this method, you'll need to grab the latest source from 'Source Code' tab on CodePlex because it wasn't in the last released version of DiscUtils.

Ken

Jun 29, 2009 at 7:51 PM

Thats Fantastic Ken! Really, thank you.

I've gone for the first option at the moment and see how it pans out.

Thanks again for such a prompt response.

Vic

Jan 29, 2013 at 4:43 PM

I know this is very old but I am having same issue. I do not have a parent disk so neither of these solutions are working for me.

 

Is there a way to extract the contents of a differencing disk without having the parent file?

 

Joey