//public List<SoftwareEntity> GetListOfInstalledSoftwares(string machineName) //{ // List<SoftwareEntity> programs = null; // string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall"; // try // { // // Open Remote Machine Registry Key // RegistryKey remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName); // RegistryKey regKey = remoteKey.OpenSubKey(softwareRegLoc); // // Open Registry Sub Key // RegistryKey subKey; // // Read Value from Registry Sub Key // string softwareName; // string displayVersion; // string installDate; // string publisher; // string estimatedSize; // programs = new List<SoftwareEntity>(); // foreach (string subKeyName in regKey.GetSubKeyNames()) // { // // Open Registry Sub Key // subKey = regKey.OpenSubKey(subKeyName); // // Read Value from Registry Sub Key // softwareName = (string)subKey.GetValue("DisplayName"); // displayVersion = (string)subKey.GetValue("DisplayVersion"); // installDate = (string)subKey.GetValue("InstallDate"); // publisher = (string)subKey.GetValue("Publisher"); // estimatedSize = subKey.GetValue("EstimatedSize") == null ? "NA" : GetSize(subKey.GetValue("EstimatedSize").ToString()); // DateTime? installationDate=new DateTime(); // if (!string.IsNullOrEmpty(installDate)) // { // DateTime dt = DateTime.ParseExact(installDate, "yyyyMMdd", CultureInfo.InvariantCulture); // installDate = dt.ToString("dd/MM/yyyy"); // installationDate = dt; // } // else // { // installationDate = null; // } // if (!string.IsNullOrEmpty(softwareName)) // { // programs.Add(new SoftwareEntity { DisplayName = softwareName, // DisplayVersion = displayVersion, // Publisher = publisher, // InstallDate = installationDate, // EstimatedSize = estimatedSize // }); // } // } // } // catch (Exception) // { // throw; // } // return programs; //} public List <SoftwareEntity> GetListOfInstalledSoftwares(string machineName) { List <SoftwareEntity> programs = new List <SoftwareEntity>(); SoftwareEntity objSoftwareEntity; var scope = new ManagementScope(); try { var options = new ConnectionOptions(); options.Authentication = AuthenticationLevel.Default; options.Impersonation = ImpersonationLevel.Impersonate; options.EnablePrivileges = true; scope = new ManagementScope(@"\\" + machineName + "\\root\\CIMV2", options); scope.Connect(); SelectQuery query = new SelectQuery("SELECT * FROM Win32_Product"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); using (ManagementObjectCollection queryCollection = searcher.Get()) { foreach (ManagementObject m in queryCollection) { objSoftwareEntity = new SoftwareEntity(); objSoftwareEntity.DisplayName = m["Caption"] == null ? "Unavailble" : m["Caption"].ToString(); objSoftwareEntity.InstallDate = m["InstallDate"] == null ? null : FormatDate(m["InstallDate"].ToString()); objSoftwareEntity.Version = m["Version"] == null ? "00.00.00.00" : m["Version"].ToString(); objSoftwareEntity.Publisher = m["Vendor"] == null ? "N/A" : m["Vendor"].ToString(); programs.Add(objSoftwareEntity); } } } catch (Exception) { return(programs); } return(programs); }
public int SaveSoftwareDetails(SoftwareEntity software) { using (_httpClient = new HttpClient()) { const string apiMethod = "SaveSoftwareDetails"; var completeUrl = _webApiurl + apiMethod + '/'; StringContent httpContent = new StringContent(JsonConvert.SerializeObject(software), Encoding.UTF8, "application/json"); var response = _httpClient.PostAsync(completeUrl, httpContent).Result; if (response.IsSuccessStatusCode) { return(Convert.ToInt32(response.Content.ReadAsStringAsync().Result)); } else { return(0); } } }