Beispiel #1
0
        static void Main(string[] args)
        {
            var options = Options.Parse(args);

            using (var logger = new Logger(options.LogFilePath))
            {
                try
                {
                    // Additional arguments parsing
                    var upgradeCode = Guid.Parse(options.UpgradeCode);
                    var maxVersion  = default(Version);
                    if (!string.IsNullOrEmpty(options.MaxVersion))
                    {
                        maxVersion = Version.Parse(options.MaxVersion);
                    }

                    var productCodes = Msi.GetRelatedProducts(upgradeCode).ToList();
                    logger.Log("Number of related products found: " + productCodes.Count);

                    foreach (var product in productCodes)
                    {
                        logger.Log("Product code: " + product);

                        if (maxVersion != null)
                        {
                            var version = Msi.GetVersion(product);
                            logger.Log("Product version: " + version);
                            if (version > maxVersion)
                            {
                                logger.Log("Skipping.");
                                continue;
                            }
                        }

                        if (options.PerUserOnly)
                        {
                            var allUsers = Msi.IsAllUsers(product);
                            logger.Log("All users: " + allUsers);
                            if (allUsers)
                            {
                                Console.WriteLine("Skipping.");
                                continue;
                            }
                        }

                        logger.Log("Uninstalling. Silently: " + options.Silent);
                        Msi.Uninstall(product, options.Silent);
                    }

                    logger.Log("Uninstall completed successfully");
                }
                catch (Exception e)
                {
                    logger.Log(e);
                    throw;
                }
            }
        }
        static int Main(string[] args)
        {
            Guid upgradeCode;

            if (args.Length < 1 || args.Length > 2 || !Guid.TryParse(args[0], out upgradeCode))
            {
                Console.WriteLine("usage:\n     UninstallRelatedProducts <GUID> <LogFile>");
                return(errUsage);
            }

            string logFile = null;

            if (args.Length == 2)
            {
                logFile = args[1];
            }

            try
            {
                using (var logger = new Logger(logFile))
                {
                    List <Guid> productCodes;
                    try
                    {
                        productCodes = Msi.GetRelatedProducts(upgradeCode).ToList();
                        logger.Log("Number of related products found: " + productCodes.Count);
                    }
                    catch (Exception ex)
                    {
                        logger.Log($"Unable to query related products: {ex.Message}");
                        return(errQuery);
                    }

                    foreach (var product in productCodes)
                    {
                        try
                        {
                            logger.Log("Product code: " + product);
                            var allUsers = Msi.IsAllUsers(product);
                            logger.Log("All users: " + allUsers);
                            if (allUsers)
                            {
                                Console.WriteLine("Skipping.");
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Log($"Failed to check if product is installed for all users: {ex.Message}");
                            // ignore this error; previous install may be corrupted. allow installation to continue
                            break;
                        }

                        try
                        {
                            Msi.Uninstall(product, silent: true);
                        }
                        catch (Exception ex)
                        {
                            logger.Log($"Failed to uninstall previous version: {ex.Message}");
                            return(errUninstall);
                        }
                    }

                    logger.Log("Legacy version check completed successfully");
                    return(0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error creating log file: {ex.Message}");
                return(errLogger);
            }
        }