DisableShowErrorUIHelper.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Microsoft.Win32;
  2. namespace ProcessDaemonTool
  3. {
  4. internal class DisableShowErrorUIHelper
  5. {
  6. static string WindowsErrorReportingLocalPath = @"SOFTWARE\Microsoft\Windows\Windows Error Reporting";
  7. static string DontShowUIKey = "DontshowUI";
  8. static string DisabledKey = "Disabled";
  9. static string LoggingDisabledKey = "LoggingDisabled";
  10. static string DontSendAdditionalDataKey = "DontSendAdditionalData";
  11. private static bool SetLocalMachineRegistryValue(string Keypath,string name,bool value)
  12. {
  13. var key = Registry.LocalMachine.OpenSubKey(Keypath, true);
  14. if (key != null)
  15. {
  16. key.SetValue(name, value?0: 1);
  17. key.Close();
  18. }
  19. return GetRegistryValue(Keypath, name);
  20. }
  21. private static bool SeCurrentUserRegistryValue(string Keypath, string name, bool value)
  22. {
  23. var key = Registry.CurrentUser.OpenSubKey(Keypath, true);
  24. if (key != null)
  25. {
  26. key.SetValue(name, value ? 0 : 1);
  27. key.Close();
  28. }
  29. return GetCurrentUserRegistryValue(Keypath, name);
  30. }
  31. private static bool GetRegistryValue(string Keypath, string name)
  32. {
  33. bool success=false;
  34. try
  35. {
  36. var key = Registry.LocalMachine.OpenSubKey(Keypath);
  37. if (key != null)
  38. {
  39. success = (int)key.GetValue(name) > 0;
  40. key.Close();
  41. }
  42. }
  43. catch (System.Exception ex)
  44. {
  45. }
  46. return success;
  47. }
  48. private static bool GetCurrentUserRegistryValue(string Keypath, string name)
  49. {
  50. bool success = false;
  51. try
  52. {
  53. var key = Registry.CurrentUser.OpenSubKey(Keypath);
  54. if (key != null)
  55. {
  56. success = (int)key.GetValue(name) > 0;
  57. key.Close();
  58. }
  59. }
  60. catch (System.Exception ex)
  61. {
  62. }
  63. return success;
  64. }
  65. public static bool SetDontShowUI()
  66. {
  67. if (SetLocalMachineRegistryValue(WindowsErrorReportingLocalPath , DontShowUIKey, false) &&
  68. SetLocalMachineRegistryValue(WindowsErrorReportingLocalPath , DisabledKey, false)&&
  69. SeCurrentUserRegistryValue(WindowsErrorReportingLocalPath, DontShowUIKey, false) &&
  70. SeCurrentUserRegistryValue(WindowsErrorReportingLocalPath, DisabledKey, false)
  71. )
  72. {
  73. return true;
  74. }
  75. return false;
  76. }
  77. }
  78. }