static void Indexer() { string indexerFile = ""; try { Guid IPropertyStoreGuid = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"); DirectoryInfo di = new DirectoryInfo(targetDirectory); bool done = false; while (!done) { foreach (FileInfo file in di.GetFiles()) { indexerFile = file.Name; // To simulate indexer, open handler directly to get fine-grained control over flags //HResult hr = (HResult)SHGetPropertyStoreFromParsingName(file.FullName, IntPtr.Zero, // GETPROPERTYSTOREFLAGS.GPS_DEFAULT, ref IPropertySto on '{indexerFile}'reGuid, out IPropertyStore ps); var ps = new IPropertyHandler(); try { HResult hr = ps.Initialize(file.FullName, (uint)(StgmConstants.STGM_READ | StgmConstants.STGM_SHARE_DENY_NONE)); if (hr == HResult.Ok) { hr = ps.GetCount(out uint propertyCount); if (hr == HResult.Ok) { for (uint index = 0; index < propertyCount; index++) { PropVariant val = new PropVariant(); hr = ps.GetAt(index, out PropertyKey key); if (hr == HResult.Ok) { hr = ps.GetValue(key, val); if (hr == HResult.Ok) { LogLine(3, $"Indexer read {val}"); } else { throw new Exception($"GetValue failed with 0x{hr:X}"); } } else { throw new Exception($"GetAt failed with 0x{hr:X}"); } } } else if ((uint)hr == 0x80030021) { LogLine(2, $"GetCount for {indexerFile} failed with STG_E_LOCKVIOLATION"); IncrementIndexerFail(); } else { throw new Exception($"GetCount failed with 0x{hr:X}"); } done = IncrementIndexer(); if (done) { break; } } else if ((uint)hr == 0x80030021) { LogLine(2, $"Open for {indexerFile} failed with STG_E_LOCKVIOLATION"); IncrementIndexerFail(); } //else // throw new Exception($"Open failed with 0x{hr:X}"); else { // Indexer can tolerate some failures, but not too many LogLine(1, $"Indexer Open failed on '{indexerFile}' with 0x{hr:X}"); IncrementIndexerFail(); } } finally { Marshal.ReleaseComObject(ps); // optional GC preempt } } } } catch (Exception ex) { LogLine(1, $"Indexer terminated on '{indexerFile}': {ex}"); IncrementIndexerTerminated(); } }