Click here to Skip to main content
Licence CPOL
First Posted 24 Mar 2006
Views 23,388
Downloads 149
Bookmarked 37 times

CD/DVD Indexer

By | 24 Mar 2006 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abdurrahman Alraies

Software Developer (Senior)

Egypt Egypt

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDisc Contents PinmemberKeith Wilson22:16 26 Mar '06  
AnswerRe: Disc Contents Pinmembereqda22:57 26 Mar '06  
GeneralRe: Disc Contents PinmemberXmen W.K.4:36 30 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 24 Mar 2006
Article Copyright 2006 by Abdurrahman Alraies
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid