How to Enable/Disable Windows Notification using C#?

崇億 張 0 Reputation points
2025-02-06T06:02:51.7433333+00:00

Hi everyone,
I want to enable/disable System > Notifications. I hope to enable or disable this option without restarting the computer.

Change this not work.
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\PushNotifications", true);

User's image

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,294 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,735 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 33,451 Reputation points Microsoft Vendor
    2025-02-06T06:40:53.4166667+00:00

    Hi @崇億 張 ,

    After modifying the registry settings, notify the system about the changes using SystemParametersInfo function.

        private const uint SPI_SETGESTURE = 0x009B;
        private const uint SPIF_SENDCHANGE = 0x02;
    
        private const uint GESTURE_ENABLE = 1;
        private const uint GESTURE_DISABLE = 0;
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
    
        static void Main(string[] args)
        {
            ModifyGestureSettings(GESTURE_ENABLE);
    
            // Notify the system about the change
            SystemParametersInfo(SPI_SETGESTURE, 0, IntPtr.Zero, SPIF_SENDCHANGE);
    
            Console.WriteLine("Gesture settings updated.");
        }
        static void ModifyGestureSettings(uint setting)
        {
            const string keyPath = @"HKEY_CURRENT_USER\Control Panel\Desktop";
            const string valueName = "TouchGestureSetting";
    
            Registry.SetValue(keyPath, valueName, setting, RegistryValueKind.DWord);
        }
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Castorix31 86,981 Reputation points
    2025-02-07T10:38:44.14+00:00

    This tests works for me on Windows 10 22H2, compiled in x64 only :

    (I just saw you tagged C#, but you can use ChatGPT to convert it...)

    					const IID CLSID_NotificationSettings = { 0xD18705BE, 0xFC2F, 0x44C8, 0xAE, 0xFF, 0x1C, 0xD4, 0x9A, 0xEA, 0x8F, 0xC1 };
    					INotificationSettings* pNS = NULL;
    					IUnknown* pUnknown;
    					HRESULT hr = CoCreateInstance(CLSID_NotificationSettings, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&pUnknown));
    					if (SUCCEEDED(hr))
    					{						
    						hr = pUnknown->QueryInterface(__uuidof(INotificationSettings), (void**)&pNS);
    						if (SUCCEEDED(hr))
    						{
    							uint32_t p0 = 0;
    							int64_t p1 = 0;
    							uint32_t p2 = 0;
    							hr = pNS->SetGlobalSetting(p0, p1, p2);
    							if (SUCCEEDED(hr))
    							{
    								MessageBox(NULL, _T("Deactivated"), _T("Information"), MB_OK | MB_ICONINFORMATION);
    							}
    							hr = pNS->GetGlobalSetting(p0, &p1);
    
    							p1 = 1;
    							hr = pNS->SetGlobalSetting(p0, p1, p2);
    							if (SUCCEEDED(hr))
    							{
    								MessageBox(NULL, _T("Activated"), _T("Information"), MB_OK | MB_ICONINFORMATION);
    							}
    						}
    					}
    
    

    with :

    
    class __declspec(uuid("bc04e8c5-c3a9-4e57-8823-5fcf927434f8")) IAumidNotificationSettings2 : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc4(/* Stack Offset: 8 */ int64_t p0);
    };
    
    class __declspec(uuid("4877a2ac-5c62-4459-9e0b-185895f37b16")) INotificationSettingsEnumerator : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ wchar_t** p0, /* Stack Offset: 16 */ int64_t* p1);
    };
    
    class __declspec(uuid("415fdd0d-b21d-4f53-bbf5-ea05bee0793f")) INotificationHandlerSettings : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc4(/* Stack Offset: 8 */ wchar_t* p0, /* Stack Offset: 16 */ int64_t* p1);
    	virtual HRESULT __stdcall Proc5(/* Stack Offset: 8 */ wchar_t* p0, /* Stack Offset: 16 */ int64_t p1);
    	virtual HRESULT __stdcall Proc6(/* Stack Offset: 8 */ INotificationSettingsEnumerator** p0);
    };
    
    class __declspec(uuid("b98a5b3d-c724-4861-9ab4-1f79582012f4")) IWNSChannelDetails : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc4(/* Stack Offset: 8 */ char** p0);
    	virtual HRESULT __stdcall Proc5(/* Stack Offset: 8 */ char** p0);
    	virtual HRESULT __stdcall Proc6(/* Stack Offset: 8 */ struct Struct_1225* p0);
    	virtual HRESULT __stdcall Proc7(/* Stack Offset: 8 */ struct Struct_1225* p0);
    	virtual HRESULT __stdcall Proc8(/* Stack Offset: 8 */ int64_t* p0);
    };
    
    class __declspec(uuid("23eb7394-4610-4807-baec-9a72f86ffa0b")) INotificationHandler : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc4(/* Stack Offset: 8 */ INotificationHandlerSettings** p0);
    	virtual HRESULT __stdcall Proc5(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc6(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc7(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc8(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc9(/* Stack Offset: 8 */ IWNSChannelDetails** p0);
    	virtual HRESULT __stdcall Proc10(/* Stack Offset: 8 */ IStream** p0);
    	virtual HRESULT __stdcall Proc11(/* Stack Offset: 8 */ IWNSChannelDetails* p0);
    	virtual HRESULT __stdcall Proc12(/* Stack Offset: 8 */ IStream* p0);
    	virtual HRESULT __stdcall Proc13(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc14(/* Stack Offset: 8 */ wchar_t* p0);
    };
    
    class __declspec(uuid("c5543b33-5c73-4dc5-9211-24077d3b06c5")) INotificationHandlerCollection : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ int64_t p0, /* Stack Offset: 16 */ INotificationHandler** p1);
    	virtual HRESULT __stdcall Proc4(/* Stack Offset: 8 */ int64_t* p0);
    };
    
    class __declspec(uuid("71d10a03-f4b1-4603-b214-e79834d10082")) IAumidNotificationSettings : public IUnknown {
    public:
    	virtual HRESULT __stdcall Proc3(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc4(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc5(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc6(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc7(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc8(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc9(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc10(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc11(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc12(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc13(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc14(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc15(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc16(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc17(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc18(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc19(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc20(/* Stack Offset: 8 */ wchar_t* p0);
    	virtual HRESULT __stdcall Proc21(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc22(/* Stack Offset: 8 */ int64_t p0);
    	virtual HRESULT __stdcall Proc23(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc24(/* Stack Offset: 8 */ IAumidNotificationSettings** p0);
    	virtual HRESULT __stdcall Proc25(/* Stack Offset: 8 */ wchar_t** p0);
    	virtual HRESULT __stdcall Proc26(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc27(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc28(/* Stack Offset: 8 */ int64_t* p0);
    	virtual HRESULT __stdcall Proc29(/* Stack Offset: 8 */ INotificationHandlerCollection** p0);
    	virtual HRESULT __stdcall Proc30();
    };
    
    class __declspec(uuid("443b1739-3779-4aba-953b-9e4ff84dd4b6")) INotificationSettings : public IUnknown {
    public:
    	virtual HRESULT __stdcall GetSettingsForAumid(/* Stack Offset: 8 */ wchar_t* p0, /* Stack Offset: 16 */ IAumidNotificationSettings** p1);
    	virtual HRESULT __stdcall GetGlobalSetting(/* Stack Offset: 8 */ /* ENUM32 */ uint32_t p0, /* Stack Offset: 16 */ int64_t* p1);
    	virtual HRESULT __stdcall SetGlobalSetting(/* Stack Offset: 8 */ /* ENUM32 */ uint32_t p0, /* Stack Offset: 16 */ int64_t p1, /* Stack Offset: 24 */ /* ENUM32 */ uint32_t p2);
    	virtual HRESULT __stdcall IsGlobalSettingRestrictedByAdmin(/* Stack Offset: 8 */ /* ENUM32 */ uint32_t p0, /* Stack Offset: 16 */ int64_t* p1);
    	virtual HRESULT __stdcall SetAumidAsOnlyOneWithThisRank(/* Stack Offset: 8 */ wchar_t* p0, /* Stack Offset: 16 */ int64_t p1, /* Stack Offset: 24 */ int64_t p2);
    	virtual HRESULT __stdcall GetFirstAumidWithRank(/* Stack Offset: 8 */ int64_t p0, /* Stack Offset: 16 */ IAumidNotificationSettings** p1);
    };
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.