Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#
Article

CD/DVD Indexer

Rate me:
Please Sign up or sign in to vote.
3.45/5 (8 votes)
24 Mar 2006CPOL2 min read 38.2K   452   38   3
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:

C#
// 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
C#
bsr.Build(drivename, fileName);

To load nodes:

C#
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:

C#
bsr.SearchAll(location, keyword);

To find a key in a specified node:

C#
bsr.Find(key, currDirNode);

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

C#
//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)


Written By
Software Developer (Senior)
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDisc Contents Pin
Keith Wilson26-Mar-06 22:16
Keith Wilson26-Mar-06 22:16 
AnswerRe: Disc Contents Pin
Abdurrahman Alraies26-Mar-06 22:57
Abdurrahman Alraies26-Mar-06 22:57 
GeneralRe: Disc Contents Pin
Xmen Real 30-Jan-10 4:36
professional Xmen Real 30-Jan-10 4:36 

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

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