Как добавить свои собственные свойства в систему свойств Windows на языке C#

#c# #windows

Вопрос:

Как я могу создать свои собственные свойства в системе свойств Windows на C#?

https://docs.microsoft.com/en-us/windows/win32/properties/property-system-overview

Среда-c# .net5, Windows 10. Я использовал программное обеспечение PropertySystemView для проверки, и следующий код смог отображаться в списке свойств.

введите описание изображения здесь

 <?xml version="1.0" encoding="utf-8"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/windows/2006/propertydescription" schemaVersion="1.0">
    <propertyDescriptionList publisher="DiscreteLogics" product="Properties">
        <propertyDescription name="WpfApp1.X" formatID="{90d13a20-4e01-4b1d-bf94-669c4113a010}" propID="10" xmlns="">
            <description>Name of a single item in the file</description>
            <searchInfo inInvertedIndex="false" isColumn="true" columnIndexType="NotIndexed" />
            <typeInfo type="String" isViewable="true" isQueryable="true" isInnate="True" multipleValues="False" />
            <labelInfo label="test test labelInfo/label" />
        </propertyDescription>
    </propertyDescriptionList>
</schema>
 
     public partial class MainWindow : Window{
        [DllImport("propsys.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
        internal static extern UInt32 PSRegisterPropertySchema(string pszPath);
        [DllImport("propsys.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
        internal static extern UInt32 PSUnregisterPropertySchema(string pszPath);
         void RegisterPropertySchema()
        {
            var filePath = "C:\xxxxxx\test.propdesc";
            UInt32 n = PSUnregisterPropertySchema(filePath);
            n = PSRegisterPropertySchema(filePath);
            MessageBox.Show($"result = 0x{n:X}");
        }
 

После этого я не знаю, как заставить произвольную логику работать с путем к файлу.
Я создал класс , который реализует IPropertyDescription для WpfApp1.X , но не было никаких доказательств того, что он был вызван.

 namespace WpfApp1{
    // https://social.msdn.microsoft.com/Forums/vstudio/ja-JP/a8954bae-296e-41fa-a596-ab7ae7906dc2/mp312398123981247912464214622447112395123881235612390?forum=wpfja
    [Flags]
    enum GETPROPERTYSTOREFLAGS
    {
        DEFAULT = 0,
        OPENSLOWITEM = 0x10,
    }

    [Flags]
    enum PROPDESC_FORMAT_FLAGS
    {
        DEFAULT = 0,
        PREFIXNAME = 0x1,
        FILENAME = 0x2,
        ALWAYSKB = 0x4,
        RESERVED_RIGHTTOLEFT = 0x8,
        SHORTTIME = 0x10,
        LONGTIME = 0x20,
        HIDETIME = 0x40,
        SHORTDATE = 0x80,
        LONGDATE = 0x100,
        HIDEDATE = 0x200,
        RELATIVEDATE = 0x400,
        USEEDITINVITATION = 0x800,
        READONLY = 0x1000,
        NOAUTOREADINGORDER = 0x2000,
    }

    [StructLayout(LayoutKind.Sequential)]
    struct PROPERTYKEY
    {
        public Guid fmtid;
        public int pid;
    }

    [Flags]
    enum VARTYPE : ushort
    {
    }

    [StructLayout(LayoutKind.Explicit)]
    struct PROPVARIANT
    {
        [FieldOffset(0)]
        public VARTYPE vt;
        [FieldOffset(8)]
        public IntPtr pwszVal;
    }

    [ComImport, Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IPropertyStore
    {
        void GetCount(out int cProps);
        void GetAt(int iProp, out PROPERTYKEY pkey);
        void GetValue([In] ref PROPERTYKEY key, out PROPVARIANT pv);
        void SetValue([In] ref PROPERTYKEY key, ref PROPVARIANT propvar);
        void Commit();
    }

    [ComImport, Guid("6f79d558-3e96-4549-a1d1-7d75d2288814"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IPropertyDescription
    {
        void GetPropertyKey(out PROPERTYKEY pkey);
        void GetCanonicalName([MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
        void GetPropertyType(out VarEnum pvartype);
        void GetDisplayName([MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
        void GetEditInvitation(out IntPtr ppszInvite);
        void GetTypeFlags([In] int mask, out int ppdtFlags);
        void GetViewFlags(out int ppdvFlags);
        void GetDefaultColumnWidth(out uint pcxChars);
        void GetDisplayType(out int pdisplaytype);
        void GetColumnState(out int pcsFlags);
        void GetGroupingRange(out int pgr);
        void GetRelativeDescriptionType(out int prdt);
        void GetRelativeDescription([In] ref PROPVARIANT propvar1, [In] ref PROPVARIANT propvar2, [MarshalAs(UnmanagedType.LPWStr)] out string ppszDesc1, [MarshalAs(UnmanagedType.LPWStr)] out string ppszDesc2);
        void GetSortDescription(out int psd);
        void GetSortDescriptionLabel([In] bool fDescending, out IntPtr ppszDescription);
        void GetAggregationType(out int paggtype);
        void GetConditionType(out int pcontype, out int popDefault);
        void GetEnumTypeList([In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out int ppv);
        void CoerceToCanonicalValue(out PROPVARIANT propvar);
        void FormatForDisplay([In] ref PROPVARIANT propvar, [In] ref int pdfFlags, [MarshalAs(UnmanagedType.LPWStr)] out string ppszDisplay);
        int IsValueCanonical([In] ref PROPVARIANT propvar);
    }
    class X : IPropertyDescription
    {
        public void GetPropertyKey(out PROPERTYKEY pkey)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetCanonicalName([MarshalAs(UnmanagedType.LPWStr)] out string ppszName)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetPropertyType(out VarEnum pvartype)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetDisplayName([MarshalAs(UnmanagedType.LPWStr)] out string ppszName)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetEditInvitation(out IntPtr ppszInvite)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetTypeFlags([In] int mask, out int ppdtFlags)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetViewFlags(out int ppdvFlags)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetDefaultColumnWidth(out uint pcxChars)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetDisplayType(out int pdisplaytype)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetColumnState(out int pcsFlags)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetGroupingRange(out int pgr)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetRelativeDescriptionType(out int prdt)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetRelativeDescription([In] ref PROPVARIANT propvar1, [In] ref PROPVARIANT propvar2, [MarshalAs(UnmanagedType.LPWStr)] out string ppszDesc1, [MarshalAs(UnmanagedType.LPWStr)] out string ppszDesc2)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetSortDescription(out int psd)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetSortDescriptionLabel([In] bool fDescending, out IntPtr ppszDescription)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetAggregationType(out int paggtype)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetConditionType(out int pcontype, out int popDefault)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void GetEnumTypeList([In] ref Guid riid, [MarshalAs(UnmanagedType.Interface), Out] out int ppv)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void CoerceToCanonicalValue(out PROPVARIANT propvar)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public void FormatForDisplay([In] ref PROPVARIANT propvar, [In] ref int pdfFlags, [MarshalAs(UnmanagedType.LPWStr)] out string ppszDisplay)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }

        public int IsValueCanonical([In] ref PROPVARIANT propvar)
        {
            MessageBox.Show($"GetPropertyKey");
            throw new NotImplementedException();
        }
    }
}
 

Комментарии:

1. Не похоже, что вы можете сделать это из .NET. От docs.microsoft.com/en-us/windows/win32/properties/… : В результате обработчики свойств не могут быть реализованы в управляемом коде и должны быть реализованы на C .