[.NET][C#] Sample of a Manager with Singleton to get info on installed software.


SUBMITTED BY: Guest

DATE: Dec. 7, 2013, 4:32 p.m.

FORMAT: C#

SIZE: 7.5 kB

HITS: 54006

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Win32;
  6. using RemoteSurvey.Ws;
  7. using RemoteSurvey.Tools;
  8. using System.Globalization;
  9. using System.IO;
  10. namespace Framework
  11. {
  12. public class ManagerComputer
  13. {
  14. #region singleton
  15. /// <summary> The instance. </summary>
  16. private static ManagerComputer instance = null;
  17. /// <summary> The padlock. </summary>
  18. private static readonly object padlock = new object();
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. /// <summary>
  21. /// Prevents a default instance of the <see cref="ManagerApplication"/> class from being created.
  22. /// </summary>
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. ManagerComputer()
  25. {
  26. //if (_Log.IsDebugEnabled)
  27. // _Log.DebugFormat("New instance of {0}", Assembly.GetExecutingAssembly());
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. /// <summary> Gets the instance. </summary>
  31. ///
  32. /// <value> The instance. </value>
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////
  34. public static ManagerComputer Instance
  35. {
  36. get
  37. {
  38. lock (padlock)
  39. {
  40. if (instance == null)
  41. {
  42. instance = new ManagerComputer();
  43. }
  44. return instance;
  45. }
  46. }
  47. }
  48. #endregion singleton
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. /// <summary>
  51. /// Gets a list of installed software and, if known, the software's install path.
  52. /// </summary>
  53. ///
  54. /// <returns> The installed software. </returns>
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////
  56. public List<Application> GetInstalledSoftware()
  57. {
  58. //Declare the string to hold the list:
  59. List<Application> lstsoftware = new List<Application>();
  60. Application software = new Application();
  61. try
  62. {
  63. //The registry key:
  64. string softwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
  65. using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(softwareKey))
  66. {
  67. //Let's go through the registry keys and get the info we need:
  68. foreach (string skName in rk.GetSubKeyNames())
  69. {
  70. using (RegistryKey sk = rk.OpenSubKey(skName))
  71. {
  72. try
  73. {
  74. //If the key has value, continue, if not, skip it:
  75. if (!(sk.GetValue("DisplayName") == null))
  76. {
  77. software.Nom = sk.GetValue("DisplayName").ToString();
  78. }
  79. //Is the install location known?
  80. if (sk.GetValue("InstallLocation") != null)
  81. {
  82. software.Path = sk.GetValue("InstallLocation").ToString();
  83. }
  84. //Is the InstallDate known?
  85. if (sk.GetValue("InstallDate") != null)
  86. {
  87. software.InstallDate = (DateTime.Parse(sk.GetValue("InstallDate").ToString(), CultureInfo.CurrentCulture)).ToShortDateString();
  88. }
  89. //Is the UninstallString known?
  90. if (sk.GetValue("UninstallString") != null)
  91. {
  92. software.UninstallString = sk.GetValue("UninstallString").ToString();
  93. }
  94. //Is the QuietUninstallString known?
  95. if (sk.GetValue("QuietUninstallString") != null)
  96. {
  97. software.QuietUninstallString = sk.GetValue("UninstallString").ToString();
  98. }
  99. //Is the Comments known?
  100. if (sk.GetValue("Comments") != null)
  101. {
  102. software.Comments = sk.GetValue("Comments").ToString();
  103. }
  104. //Is the DisplayVersion known?
  105. if (sk.GetValue("DisplayVersion") != null)
  106. {
  107. software.Version = sk.GetValue("DisplayVersion").ToString();
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. //No, that exception is not getting away... :P
  113. }
  114. }
  115. lstsoftware.Add(software);
  116. }
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. ManagerLogger.Instance.LogException("RemoteSurvey", ex);
  122. }
  123. return lstsoftware;
  124. }
  125. ////////////////////////////////////////////////////////////////////////////////////////////////////
  126. /// <summary> Check if a software is installed. </summary>
  127. ///
  128. /// <param name="psSoftwareName"> The ps software. </param>
  129. ///
  130. /// <returns>
  131. /// <c>true</c> if [is software installed] [the specified ps software]; otherwise, <c>false</c>.
  132. /// </returns>
  133. ////////////////////////////////////////////////////////////////////////////////////////////////////
  134. public bool IsSoftwareInstalled(string psSoftwareName)
  135. {
  136. List<Application> lst = new List<Application>();
  137. Boolean bReturn = false;
  138. try
  139. {
  140. lst = GetInstalledSoftware();
  141. if (lst.Where(p => p.Nom.ToUpper().Equals(psSoftwareName.ToUpper())).Count() > 0)
  142. {
  143. bReturn = true;
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. ManagerLogger.Instance.LogException("RemoteSurvey", ex);
  149. }
  150. return bReturn;
  151. }
  152. }
  153. }

comments powered by Disqus