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
/// The instance.
private static ManagerComputer instance = null;
/// The padlock.
private static readonly object padlock = new object();
////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Prevents a default instance of the class from being created.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
ManagerComputer()
{
//if (_Log.IsDebugEnabled)
// _Log.DebugFormat("New instance of {0}", Assembly.GetExecutingAssembly());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the instance.
///
/// The instance.
////////////////////////////////////////////////////////////////////////////////////////////////////
public static ManagerComputer Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new ManagerComputer();
}
return instance;
}
}
}
#endregion singleton
////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Gets a list of installed software and, if known, the software's install path.
///
///
/// The installed software.
////////////////////////////////////////////////////////////////////////////////////////////////////
public List GetInstalledSoftware()
{
//Declare the string to hold the list:
List lstsoftware = new List();
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Check if a software is installed.
///
/// The ps software.
///
///
/// true if [is software installed] [the specified ps software]; otherwise, false.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool IsSoftwareInstalled(string psSoftwareName)
{
List lst = new List();
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;
}
}
}