public bool Initialize() { if (!File.Exists("ZuneDBApi.dll")) { throw new FileNotFoundException( "Could not find ZuneDBApi.dll. Are you sure Zune Social Tagger is installed in the Zune application folder?"); } //Just copying what the zune software does internally here to initialize the database _zuneLibrary = new ZuneLibrary(); bool dbRebult; //anything other than 0 means an error occured reading the database int num = _zuneLibrary.Initialize(null, out dbRebult); if (num > -1) { int phase2; _zuneLibrary.Phase2Initialization(out phase2); _zuneLibrary.CleanupTransientMedia(); } else { return(false); } return(true); }
// Using http://blog.ctaggart.com/2010/08/query-zune-music-collection-with-f.html#!/2010/08/query-zune-music-collection-with-f.html as a base // Also found some help at http://averagedeveloper.blogspot.com/2012/04/querying-zunedbapidll.html /// <summary> /// Reindexes the music by iterating through all music in the Zune Library and saving it in the local Lucene database. Required for search to work correctly /// </summary> /// <seealso cref="Search"/> public void ReIndexMusic() { var library = new ZuneLibrary(); var dbReloaded = false; int returnValue = library.Initialize(null, out dbReloaded); if (returnValue >= 0) { library.Phase2Initialization(out returnValue); if (returnValue >= 0) { library.CleanupTransientMedia(); ZuneQueryList searchResult = library.QueryDatabase(EQueryType.eQueryTypeAllTracks, 0, EQuerySortType.eQuerySortOrderNone, 0, null); if (null != searchResult) { // we have results, index them var writer = new IndexWriter(new SimpleFSDirectory(new DirectoryInfo(LUCENE_INDEX_DIRECTORY)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED); for (uint i = 0; i < searchResult.Count; i++) { // add a document for each song. We will only store the mediaId and mediaTypeId and store+index the title, artist, and album var doc = new Document(); var mediaId = (UInt32)searchResult.GetFieldValue(i, typeof(UInt32), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_MediaID); var mediaTypeId = (UInt32)searchResult.GetFieldValue(i, typeof(UInt32), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_MediaType); var duration = (UInt32)searchResult.GetFieldValue(i, typeof(UInt32), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_Duration); var artist = (String)searchResult.GetFieldValue(i, typeof(String), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_DisplayArtist); var title = (String)searchResult.GetFieldValue(i, typeof(String), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_Title); var album = (String)searchResult.GetFieldValue(i, typeof(String), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_WMAlbumTitle); doc.Add(new Field("mediaId", mediaId.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("mediaTypeId", mediaTypeId.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("duration", duration.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("artist", artist, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("album", album, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); } writer.Commit(); writer.Close(); } } } }