Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CD/DVD Indexer

0.00/5 (No votes)
24 Mar 2006 1  
Save the CD/DVD file properties to computer
Sample Image - dvdindexer.png

Introduction

After years of backing up my files on CDs, I found that I cannot remember the CD so I have to choose some suspicious CDs and search for my files on it! This program can save the CD/DVD file properties to the PC so that when I want to search for a file, I need not search every CD. All I need to do is search for the specified file by entering the key and click the Search button. The code does this in the following manner:

  1. To get and save data:
    1. Gets all file names with its primary properties and directories
    2. Saves this data to DireNode and FileNode objects
    3. Serializes the data to a stream
    4. Compresses the serialized stream and saves it to a file
  2. To retrieve data:
    1. Decompresses the data
    2. Deserializes data and links it to a treeview control
  3. To search loop on all files in the specified directory:
    1. Decompresses the data
    2. Deserializes data
    3. Searches using the keyword, if found shows the results in the treeview

Using the Code

To use the code:

// Define these variables

        BuildSaveAndRetrieve bsr;
        bool search;

Use this method to build the file.

  • The first param is the selected DVD drive
  • The second param is the full file name to save the data
bsr.Build(drivename, fileName);

To load nodes:

bsr.LoadNodes(fileName, out initNode);
  • The first parameter is the file name where the needed data is saved
  • The second is the treenode where the data is retrieved

To search for a key in a directory, the first parameter is the location of the data files, the second is for the keyword:

bsr.SearchAll(location, keyword);

To find a key in a specified node:

bsr.Find(key, currDirNode);

You can also use compression and decompression. For example (De)Compress, (De)Serialization:

//decompression
        public Stream DeflateDecompress(string fileName)
        {
            try
            {
                FileStream fs= new FileStream(fileName, FileMode.Open, 
                    FileAccess.Read, FileShare.None);

                return (new DeflateStream(fs, CompressionMode.Decompress));
            }
            catch (Exception e)
            {
                ErrorMessage(e.Message);
                return null;
            }
        }
//Compression & test
public bool DeflateCompress(Stream ms, string fileName)
        {
            try
            {
                byte[] buffer = new byte[ms.Length];

                ms.Position = 0;

                if ((ms.Read(buffer, 0, buffer.Length)) != ms.Length)
                {
                    ErrorMessage("Unable to read data from memory");
                    return false;
                }
                ms.Close();

                FileStream fs = new FileStream(fileName, FileMode.Create, 
                    FileAccess.Write, FileShare.None);

                DeflateStream compressedzipStream = new DeflateStream
                    (fs, CompressionMode.Compress, true);

                compressedzipStream.Write(buffer, 0, buffer.Length);
                compressedzipStream.Close();
                fs.Close();

                fs = new FileStream(fileName, FileMode.Open);
                DeflateStream zipStream = new DeflateStream
                    (fs, CompressionMode.Decompress);

                if (!TestData(buffer, zipStream))
                {
                    zipStream.Close();
                    return false;
                }
                else
                {
                    zipStream.Close();
                    return true;
                }
            }
            catch (Exception e)
            {
                ErrorMessage(e.Message);
                return false;
            }
        }

//Test
        public static bool TestData(byte[] bufferSrc, Stream decompressed)
        {
            int bufferSrcLength = bufferSrc.Length;
            int decompressedStreamLength = 0;
            byte[] buffer = new byte[bufferSrcLength];
            decompressedStreamLength = decompressed.Read(buffer, 0, bufferSrcLength);

            if (decompressedStreamLength != bufferSrcLength)
                return false;

            for (int i = 0; i < decompressedStreamLength; i++)
            {
                if (bufferSrc[i] != buffer[i])
                    return false;
            }
            return true;
        }
//Serialization
private Stream SerializeNodes(string fileName, DirNode rootCDDVDDir)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, rootCDDVDDir);
            return ms;
        }

//Deserialization
        private static DirNode DeserializeNodes(Stream s)
        {
            BinaryFormatter bf = new BinaryFormatter();
            try 
            { 
                DirNode retrievedRootDirNode = (DirNode)bf.Deserialize(s);
                s.Close();
                return retrievedRootDirNode;
            }
            catch (Exception e)
            {
                ErrorMessage(e.Message);
                s.Close();
                return null;
            }
        }

Points of Interest

The important things are these methods above and the two class definitions. The other important things are the Save, Retrieve, Find and SearchAll methods.

The code in the project is more descriptive than my words (I think) because it is my first article.
I'm waiting for your feedback.
That is all.
Hope you enjoy the code.

History

  • 24th March, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here