using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using RemoteSurvey.Ws;
using RemoteSurvey.Tools;
using System.Globalization;
using System.IO;
namespace Framework
{
public class ManagerComputer
{
#region singleton
/// <summary> The instance. </summary>
private static ManagerComputer instance = null;
/// <summary> The padlock. </summary>
private static readonly object padlock = new object();
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Prevents a default instance of the <see cref="ManagerApplication"/> class from being created.
/// </summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
ManagerComputer()
{
//if (_Log.IsDebugEnabled)
// _Log.DebugFormat("New instance of {0}", Assembly.GetExecutingAssembly());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the instance. </summary>
///
/// <value> The instance. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static ManagerComputer Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new ManagerComputer();
}
return instance;
}
}
}
#endregion singleton
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets a list of installed software and, if known, the software's install path.
/// </summary>
///
/// <returns> The installed software. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public List<Application> GetInstalledSoftware()
{
//Declare the string to hold the list:
List<Application> lstsoftware = new List<Application>();
Application software = new Application();
try
{
//The registry key:
string softwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(softwareKey))
{
//Let's go through the registry keys and get the info we need:
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
//If the key has value, continue, if not, skip it:
if (!(sk.GetValue("DisplayName") == null))
{
software.Nom = sk.GetValue("DisplayName").ToString();
}
//Is the install location known?
if (sk.GetValue("InstallLocation") != null)
{
software.Path = sk.GetValue("InstallLocation").ToString();
}
//Is the InstallDate known?
if (sk.GetValue("InstallDate") != null)
{
software.InstallDate = (DateTime.Parse(sk.GetValue("InstallDate").ToString(), CultureInfo.CurrentCulture)).ToShortDateString();
}
//Is the UninstallString known?
if (sk.GetValue("UninstallString") != null)
{
software.UninstallString = sk.GetValue("UninstallString").ToString();
}
//Is the QuietUninstallString known?
if (sk.GetValue("QuietUninstallString") != null)
{
software.QuietUninstallString = sk.GetValue("UninstallString").ToString();
}
//Is the Comments known?
if (sk.GetValue("Comments") != null)
{
software.Comments = sk.GetValue("Comments").ToString();
}
//Is the DisplayVersion known?
if (sk.GetValue("DisplayVersion") != null)
{
software.Version = sk.GetValue("DisplayVersion").ToString();
}
}
catch (Exception ex)
{
//No, that exception is not getting away... :P
}
}
lstsoftware.Add(software);
}
}
}
catch (Exception ex)
{
ManagerLogger.Instance.LogException("RemoteSurvey", ex);
}
return lstsoftware;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Check if a software is installed. </summary>
///
/// <param name="psSoftwareName"> The ps software. </param>
///
/// <returns>
/// <c>true</c> if [is software installed] [the specified ps software]; otherwise, <c>false</c>.
/// </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool IsSoftwareInstalled(string psSoftwareName)
{
List<Application> lst = new List<Application>();
Boolean bReturn = false;
try
{
lst = GetInstalledSoftware();
if (lst.Where(p => p.Nom.ToUpper().Equals(psSoftwareName.ToUpper())).Count() > 0)
{
bReturn = true;
}
}
catch (Exception ex)
{
ManagerLogger.Instance.LogException("RemoteSurvey", ex);
}
return bReturn;
}
}
}