private bool AddDirectoryEx(DvMediaContainer container, DirectoryInfo directory)
        {
            if (directory.Exists == false) return false;

            MediaBuilder.storageFolder storageInfo = new MediaBuilder.storageFolder(directory.Name);
            storageInfo.Searchable = true;
            storageInfo.IsRestricted = false;
            DvMediaContainer newContainer = DvMediaBuilder.CreateContainer(storageInfo);
            newContainer.OnChildrenRemoved += new DvDelegates.Delegate_OnChildrenRemove(this.Sink_OnChildRemoved);
            newContainer.Callback_UpdateMetadata = new DvMediaContainer.Delegate_UpdateMetadata(this.Sink_UpdateContainerMetadata);

            InnerMediaDirectory mediadir = new InnerMediaDirectory();
            mediadir.directory = directory;
            mediadir.directoryname = directory.FullName;
            mediadir.watcher = new FileSystemWatcher(directory.FullName);
            mediadir.watcher.Changed += new FileSystemEventHandler(OnDirectoryChangedSink);
            mediadir.watcher.Created += new FileSystemEventHandler(OnDirectoryCreatedSink);
            mediadir.watcher.Deleted += new FileSystemEventHandler(OnDirectoryDeletedSink);
            mediadir.watcher.Renamed += new RenamedEventHandler(OnFileSystemRenameSink);
            mediadir.restricted = true;
            mediadir.readOnly = true;

            watcherTable.Add(mediadir.watcher,newContainer);
            mediadir.watcher.EnableRaisingEvents = true;

            newContainer.Tag = mediadir;

            FileInfo[] files = directory.GetFiles();
            ArrayList addedFiles = new ArrayList(files.Length);
            foreach (FileInfo file in files)
            {
                IDvMedia newObj = CreateObjFromFile(file, new ArrayList());
                if (newObj != null)
                {
                    addedFiles.Add(newObj);
                    totalFileCount++;
                }
            }

            newContainer.AddObjects(addedFiles, true);

            // Add the new container to an existing container.
            container.AddObject(newContainer, true);

            foreach (IDvMedia item in addedFiles)
            {
                if (item.Class.IsA(MediaBuilder.StandardMediaClasses.AudioItem))
                {
                    this.m_AudioItems.AddReference((DvMediaItem)item);
                }
                else if (item.Class.IsA(MediaBuilder.StandardMediaClasses.ImageItem))
                {
                    this.m_ImageItems.AddReference((DvMediaItem)item);
                }
                else if (item.Class.IsA(MediaBuilder.StandardMediaClasses.VideoItem))
                {
                    this.m_VideoItems.AddReference((DvMediaItem)item);
                }
                else if (item.Class.IsA(MediaBuilder.StandardMediaClasses.PlaylistContainer))
                {
                    // References to playlists are not allowed, so we have to build
                    // completely new instances of the container and its resources.
                    // However the new container can have references to the items.
                    DvMediaContainer originalC = (DvMediaContainer) item;

                    MediaBuilder.playlistContainer plcInfo = new MediaBuilder.playlistContainer(originalC.Title);
                    DvMediaContainer newC = DvMediaBuilder.CreateContainer(plcInfo);

                    foreach (DvMediaResource res in originalC.Resources)
                    {
                        ResourceBuilder.AllResourceAttributes resAttribs = new ResourceBuilder.AllResourceAttributes();
                        resAttribs.contentUri = res.ContentUri;
                        foreach (string attribute in res.ValidAttributes)
                        {
                            object obj = res[attribute];
                            _RESATTRIB attribName = (_RESATTRIB) Enum.Parse(typeof(_RESATTRIB), attribute, true);

                            switch (attribName)
                            {
                                case _RESATTRIB.bitrate:
                                    resAttribs.bitrate = (_UInt) obj;
                                    break;
                                case _RESATTRIB.bitsPerSample:
                                    resAttribs.bitsPerSample = (_UInt) obj;
                                    break;
                                case _RESATTRIB.colorDepth:
                                    resAttribs.colorDepth = (_UInt) obj;
                                    break;
                                case _RESATTRIB.duration:
                                    resAttribs.duration = (_TimeSpan) obj;
                                    break;
                                case _RESATTRIB.importUri:
                                    //do not allow import
                                    break;
                                case _RESATTRIB.nrAudioChannels:
                                    resAttribs.nrAudioChannels = (_UInt) obj;
                                    break;
                                case _RESATTRIB.protection:
                                    resAttribs.protection = (string) obj;
                                    break;
                                case _RESATTRIB.protocolInfo:
                                    resAttribs.protocolInfo = new ProtocolInfoString(((ProtocolInfoString)obj).ToString());
                                    break;
                                case _RESATTRIB.resolution:
                                    resAttribs.resolution = (ImageDimensions) obj;
                                    break;
                                case _RESATTRIB.sampleFrequency:
                                    resAttribs.sampleFrequency = (_UInt) obj;
                                    break;
                                case _RESATTRIB.size:
                                    resAttribs.size = (_ULong) obj;
                                    break;
                            }
                        }
                        DvMediaResource newCR = DvResourceBuilder.CreateResource(resAttribs, false);
                        newCR.AllowImport = res.AllowImport;
                        newCR.CheckAutomapFileExists = res.CheckAutomapFileExists;
                        newCR.HideContentUri = res.HideContentUri;
                        newCR.MakeStreamAtHttpGetTime = res.MakeStreamAtHttpGetTime;
                        newCR.Tag = res.Tag;

                        newC.AddResource(newCR);
                    }

                    // The child container should only have items.
                    // If the child-container has child containers, then
                    // then this is bad because those child playlists
                    // should have been converted to a flat list
                    // in the recursive building of the hierarchy
                    foreach (DvMediaItem childItem in originalC.CompleteList)
                    {
                        newC.AddReference(childItem);
                    }

                    this.m_Playlists.AddObject(newC, true);
                }
            }

            DirectoryInfo[] directories = directory.GetDirectories();
            foreach (DirectoryInfo dir in directories)
            {
                AddDirectoryEx(newContainer,dir);
            }

            totalDirectoryCount++;
            if (OnStatsChanged != null) OnStatsChanged(this);
            return true;
        }
        private DvMediaContainer CreateM3uPlaylistContainer(FileInfo file, ArrayList childPlaylists)
        {
            // prevent an infinite loop of playlists containing each other
            if (childPlaylists.Contains(file.Name) == false)
            {
                childPlaylists.Add(file.Name);
            }
            else
            {
                return null;
            }

            string mime, mediaClass;
            MimeTypes.ExtensionToMimeType(file.Extension, out mime, out mediaClass);

            string title = Path.GetFileNameWithoutExtension(file.Name);
            string protInfo = new System.Text.StringBuilder().AppendFormat("http-get:*:{0}:*", mime).ToString();

            MediaBuilder.playlistContainer info = new MediaBuilder.playlistContainer(title);
            DvMediaContainer newPC = DvMediaBuilder.CreateContainer(info);
            newPC.Tag = file;

            // parse the M3U playlist

            StreamReader sr = File.OpenText(file.FullName);
            while (sr.Peek() > -1)
            {
                string itemPath = sr.ReadLine();

                if (Directory.Exists(itemPath) == false)
                {
                    if (File.Exists(itemPath) == true)
                    {
                        FileInfo fi = new FileInfo(itemPath);
                        IDvMedia playlistItem = this.CreateObjFromFile(fi, childPlaylists);

                        playlistItem.WriteStatus = EnumWriteStatus.NOT_WRITABLE;
                        playlistItem.IsRestricted = true;

                        if (playlistItem != null)
                        {
                            if (playlistItem.IsItem)
                            {
                                // This is a child item.
                                DvMediaItem childItem = (DvMediaItem) playlistItem;
                                newPC.AddObject(childItem, true);
                            }
                            else
                            {
                                // This is a child playlist, but we want to
                                // flatten a hierarchy of playlists, so save
                                // the references to the children,
                                // remove them from the original container,
                                // and add them to the current container.

                                DvMediaContainer childPlaylist = (DvMediaContainer) playlistItem;
                                IList children = childPlaylist.CompleteList;
                                childPlaylist.RemoveObjects(children);
                                newPC.AddObjects(children, true);
                            }
                        }
                    }
                }
            }
            sr.Close();

            //TODO: add playlist resource
            // add a resource that will represent the playlist as an m3u
            DvMediaResource newRes = this.BuildM3uResource(file, protInfo);
            newPC.AddResource(newRes);

            return newPC;
        }