| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | using System; |
| 4 | using System.Collections; |
| 5 | using WixToolset.Dtf.WindowsInstaller; |
| 6 | |
| 7 | |
| 8 | namespace WixToolset.Dtf.Tools.Inventory |
| 9 | { |
| 10 | public class MsiUtils |
| 11 | { |
| 12 | private static Hashtable productCodesToNames = new Hashtable(); |
| 13 | private static Hashtable productNamesToCodes = new Hashtable(); |
| 14 | |
| 15 | public static string GetProductName(string productCode) |
| 16 | { |
| 17 | string productName = (string) productCodesToNames[productCode]; |
| 18 | if(productName == null) |
| 19 | { |
| 20 | productName = new ProductInstallation(productCode).ProductName; |
| 21 | productName = productName.Replace('\\', ' '); |
| 22 | if(productNamesToCodes.Contains(productName)) |
| 23 | { |
| 24 | string modifiedProductName = null; |
| 25 | for(int i = 2; i < Int32.MaxValue; i++) |
| 26 | { |
| 27 | modifiedProductName = productName + " [" + i + "]"; |
| 28 | if(!productNamesToCodes.Contains(modifiedProductName)) break; |
| 29 | } |
| 30 | productName = modifiedProductName; |
| 31 | } |
| 32 | productCodesToNames[productCode] = productName; |
| 33 | productNamesToCodes[productName] = productCode; |
| 34 | } |
| 35 | return productName; |
| 36 | } |
| 37 | |
| 38 | // Assumes GetProductName() has already been called for this product. |
| 39 | public static string GetProductCode(string productName) |
| 40 | { |
| 41 | return (string) productNamesToCodes[productName]; |
| 42 | } |
| 43 | |
| 44 | private MsiUtils() { } |
| 45 | } |
| 46 | } |