|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace FileMetadataAssociationManager |
| 10 | +{ |
| 11 | + static class PropertyHandlerTest |
| 12 | + { |
| 13 | + private static Guid IPropertyStoreGuid = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"); |
| 14 | + |
| 15 | + public static bool WindowsIgnoresOurPropertyHandler(string extension) |
| 16 | + { |
| 17 | + // Create a temporary file with the right extension |
| 18 | + string fileFullName = Path.ChangeExtension( |
| 19 | + Path.GetTempPath() + Guid.NewGuid().ToString(), extension); |
| 20 | + using (var fs = File.Create(fileFullName)) { } |
| 21 | + |
| 22 | + // Get the property store that Explorer would use |
| 23 | + IPropertyStore ps; |
| 24 | + bool result = true; |
| 25 | + HResult hr = (HResult)SHGetPropertyStoreFromParsingName(fileFullName, IntPtr.Zero, |
| 26 | + GETPROPERTYSTOREFLAGS.GPS_NO_OPLOCK | GETPROPERTYSTOREFLAGS.GPS_HANDLERPROPERTIESONLY, ref IPropertyStoreGuid, out ps); |
| 27 | + if (hr == HResult.Ok) |
| 28 | + { |
| 29 | + // Look for the signature property value that marks the handler as ours |
| 30 | + PropertyKey key; |
| 31 | + PropVariant pv = new PropVariant(); |
| 32 | + PropertySystemNativeMethods.PSGetPropertyKeyFromName("System.Software.ProductName", out key); |
| 33 | + hr = ps.GetValue(key, pv); |
| 34 | + if (hr == HResult.Ok && !pv.IsNullOrEmpty) |
| 35 | + { |
| 36 | + if ((pv.Value as string) == "FileMetadata") |
| 37 | + result = false; |
| 38 | + } |
| 39 | + Marshal.ReleaseComObject(ps); // This release frees up the file for deletion |
| 40 | + } |
| 41 | + |
| 42 | + File.Delete(fileFullName); |
| 43 | + |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + [DllImport("shell32.dll", SetLastError = true)] |
| 48 | + public static extern int SHGetPropertyStoreFromParsingName( |
| 49 | + [In][MarshalAs(UnmanagedType.LPWStr)] string pszPath, |
| 50 | + IntPtr zeroWorks, |
| 51 | + GETPROPERTYSTOREFLAGS flags, |
| 52 | + ref Guid iIdPropStore, |
| 53 | + [Out] out IPropertyStore propertyStore); |
| 54 | + } |
| 55 | + |
| 56 | + public enum GETPROPERTYSTOREFLAGS |
| 57 | + { |
| 58 | + // If no flags are specified (GPS_DEFAULT), a read-only property store is returned that includes properties for the file or item. |
| 59 | + // In the case that the shell item is a file, the property store contains: |
| 60 | + // 1. properties about the file from the file system |
| 61 | + // 2. properties from the file itself provided by the file's property handler, unless that file is offline, |
| 62 | + // see GPS_OPENSLOWITEM |
| 63 | + // 3. if requested by the file's property handler and supported by the file system, properties stored in the |
| 64 | + // alternate property store. |
| 65 | + // |
| 66 | + // Non-file shell items should return a similar read-only store |
| 67 | + // |
| 68 | + // Specifying other GPS_ flags modifies the store that is returned |
| 69 | + GPS_DEFAULT = 0x00000000, |
| 70 | + GPS_HANDLERPROPERTIESONLY = 0x00000001, // only include properties directly from the file's property handler |
| 71 | + GPS_READWRITE = 0x00000002, // Writable stores will only include handler properties |
| 72 | + GPS_TEMPORARY = 0x00000004, // A read/write store that only holds properties for the lifetime of the IShellItem object |
| 73 | + GPS_FASTPROPERTIESONLY = 0x00000008, // do not include any properties from the file's property handler (because the file's property handler will hit the disk) |
| 74 | + GPS_OPENSLOWITEM = 0x00000010, // include properties from a file's property handler, even if it means retrieving the file from offline storage. |
| 75 | + GPS_DELAYCREATION = 0x00000020, // delay the creation of the file's property handler until those properties are read, written, or enumerated |
| 76 | + GPS_BESTEFFORT = 0x00000040, // For readonly stores, succeed and return all available properties, even if one or more sources of properties fails. Not valid with GPS_READWRITE. |
| 77 | + GPS_NO_OPLOCK = 0x00000080, // some data sources protect the read property store with an oplock, this disables that |
| 78 | + GPS_MASK_VALID = 0x000000FF, |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// A property store |
| 83 | + /// </summary> |
| 84 | + [ComImport] |
| 85 | + [Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99")] |
| 86 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 87 | + interface IPropertyStore |
| 88 | + { |
| 89 | + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] |
| 90 | + HResult GetCount([Out] out uint propertyCount); |
| 91 | + |
| 92 | + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] |
| 93 | + HResult GetAt([In] uint propertyIndex, out PropertyKey key); |
| 94 | + |
| 95 | + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] |
| 96 | + HResult GetValue([In] ref PropertyKey key, [Out] PropVariant pv); |
| 97 | + |
| 98 | + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), PreserveSig] |
| 99 | + HResult SetValue([In] ref PropertyKey key, [In] PropVariant pv); |
| 100 | + |
| 101 | + [PreserveSig] |
| 102 | + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] |
| 103 | + HResult Commit(); |
| 104 | + } |
| 105 | + |
| 106 | + [ComImport] |
| 107 | + [Guid("C8E2D566-186E-4D49-BF41-6909EAD56ACC")] |
| 108 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 109 | + interface IPropertyStoreCapabilities |
| 110 | + { |
| 111 | + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] |
| 112 | + HResult IsPropertyWritable([In]ref PropertyKey propertyKey); |
| 113 | + } |
| 114 | +} |
0 commit comments