public async Task<OfficeInstallation> CheckForOfficeInstallAsync()
        {
            var officeInstance = new OfficeInstallation() { Installed = false };
            string readtext = "";
            try
            {
                
                string PSPath = System.IO.Path.GetTempPath() + remoteComputerName + "PowershellAttemptVersion.txt";
                System.IO.File.Delete(PSPath);
                using (var powerShellInstance = System.Management.Automation.PowerShell.Create())
                {
                    powerShellInstance.AddScript(System.IO.Directory.GetCurrentDirectory() + "\\Resources\\FindVersion.ps1 -machineToRun " + remoteComputerName);
                    var async = powerShellInstance.Invoke();
                }
                readtext = System.IO.File.ReadAllText(PSPath);
                readtext = readtext.Trim();

                officeInstance.Version = readtext.Split('\\')[0];
            
                if (!string.IsNullOrEmpty(officeInstance.Version))
                {
                    officeInstance.Installed = true;
                    var currentBaseCDNUrl = readtext.Split('\\')[1];


                    var installFile = await GetOfficeInstallFileXml();
                    if (installFile == null) return officeInstance;

                    var currentBranch = installFile.BaseURL.FirstOrDefault(b => b.URL.Equals(currentBaseCDNUrl) &&
                                                                                !b.Branch.ToLower().Contains("business"));
                    if (currentBranch != null)
                    {
                        officeInstance.Channel = currentBranch.Branch;

                        var latestVersion =
                            await GetOfficeLatestVersion(currentBranch.Branch, OfficeEdition.Office32Bit);
                        officeInstance.LatestVersion = latestVersion;
                    }


                }
            }
            catch (Exception ex)
            {
                using (System.IO.StreamWriter file =
           new System.IO.StreamWriter(System.IO.Path.GetTempPath() + "failure.txt", true))
                {
                    file.WriteLine(ex.Message);
                }
                throw new Exception(ex.Message);
            }
            return officeInstance;
        }
        public async Task<OfficeInstallation> CheckForOfficeInstallAsync()
        {
            var localInstall = new OfficeInstallation()
            {
                Installed = false
            };

            var officeRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Office\ClickToRun\Configuration");
            if (officeRegKey == null)
            {
                officeRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Office\16.0\ClickToRun\Configuration");
                if (officeRegKey == null)
                {
                    officeRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Office\15.0\ClickToRun\Configuration");
                }
            }
            if (officeRegKey != null)
            {
                localInstall.Version = GetRegistryValue(officeRegKey, "VersionToReport");
                if (string.IsNullOrEmpty(localInstall.Version)) return localInstall;

                localInstall.Installed = true;
                
                var currentBaseCDNUrl = GetRegistryValue(officeRegKey, "CDNBaseUrl");
 
                var installFile = await GetOfficeInstallFileXml();
                if (installFile == null) return localInstall;

                var currentBranch = installFile.BaseURL.FirstOrDefault(b => b.URL.Equals(currentBaseCDNUrl) &&
                                                                            !b.Branch.ToLower().Contains("business"));
                if (currentBranch != null)
                {
                    localInstall.Channel = currentBranch.Branch;

                    var latestVersion = await GetOfficeLatestVersion(currentBranch.Branch, OfficeEdition.Office32Bit);
                    localInstall.LatestVersion = latestVersion;
                }
            }

            return localInstall;
        }
        public async Task<OfficeInstallation> CheckForOfficeInstallAsync()
        {         
            var officeInstance = new OfficeInstallation() { Installed = false };
            var officeRegPathKey = @"SOFTWARE\Microsoft\Office\ClickToRun\Configuration";

            officeInstance.Version = await GetRegistryValue(officeRegPathKey, "VersionToReport", "GetStringValue");

            if (string.IsNullOrEmpty(officeInstance.Version))
                {
                    officeRegPathKey = @"SOFTWARE\Microsoft\Office\16.0\ClickToRun\Configuration";
                officeInstance.Version = await GetRegistryValue(officeRegPathKey, "VersionToReport", "GetStringValue");

                    if (string.IsNullOrEmpty(officeInstance.Version))
                    {
                        officeRegPathKey = @"SOFTWARE\Microsoft\Office\15.0\ClickToRun\Configuration";
                        officeInstance.Version = await GetRegistryValue(officeRegPathKey, "VersionToReport", "GetStringValue");

                    }

                }

                if(!string.IsNullOrEmpty(officeInstance.Version))
                {
                    officeInstance.Installed = true;
                    var currentBaseCDNUrl = await GetRegistryValue(officeRegPathKey, "CDNBaseUrl", "GetStringValue");


                    var installFile = await GetOfficeInstallFileXml();
                    if (installFile == null) return officeInstance;

                    var currentBranch = installFile.BaseURL.FirstOrDefault(b => b.URL.Equals(currentBaseCDNUrl) &&
                                                                          !b.Branch.ToLower().Contains("business"));
                    if (currentBranch != null)
                    {
                        officeInstance.Channel = currentBranch.Branch;

                        var latestVersion = await GetOfficeLatestVersion(currentBranch.Branch, OfficeEdition.Office32Bit);
                        officeInstance.LatestVersion = latestVersion;
                    }
            }

            return officeInstance;
 
        }
        public async Task<OfficeInstallation> CheckForOfficeInstallAsync()
        {
            var result = new OfficeInstallation();

            if (isLocal)
            {
                result = await LocalInstall.CheckForOfficeInstallAsync();
            }
            else
            {
                switch (_connectionType)
                {
                    case ConnectionType.WMI:
                        result = await WmiInstall.CheckForOfficeInstallAsync();
                        break;
                    case ConnectionType.PowerShell:
                        result = await PowershellInstall.CheckForOfficeInstallAsync();
                        break;
                    default:
                        throw new Exception("Connection Unknown");
                }
            }

            return result;
        }
        public async Task ChangeOfficeChannel()
        {
            await Task.Run(async () =>
            {
                var installOffice = new InstallOffice();
                try
                {
                    installOffice = new InstallOffice();
                    installOffice.UpdatingOfficeStatus += installOffice_UpdatingOfficeStatus;

                    var newChannel = "";
                    Dispatcher.Invoke(() =>
                    {
                        UpdateStatus.Content = "Updating...";
                        newChannel = ((OfficeBranch) ProductBranch.SelectedItem).NewName;
                        ChangeChannel.IsEnabled = false;
                        NewVersion.IsEnabled = false;
                    });

                    SetItemState(LocalViewItem.Update, LocalViewState.Wait);
                    
                    var ppDownloader = new ProPlusDownloader();
                    var baseUrl = await ppDownloader.GetChannelBaseUrlAsync(newChannel, OfficeEdition.Office32Bit);
                    if (string.IsNullOrEmpty(baseUrl))
                        throw (new Exception(string.Format("Cannot find BaseUrl for Channel: {0}", newChannel)));

                    var channelToChangeTo = "";
                    if (NewVersionRow.Visibility != Visibility.Visible)
                    {
                        channelToChangeTo =
                            await ppDownloader.GetLatestVersionAsync(newChannel, OfficeEdition.Office32Bit);
                    }
                    else
                    {
                        Dispatcher.Invoke(() =>
                        {
                            var manualVersion = NewVersion.Text;

                            if (string.IsNullOrEmpty(manualVersion) && NewVersion.SelectedItem != null)
                            {
                                manualVersion = ((Build)NewVersion.SelectedItem).Version;
                            }
                            if (!string.IsNullOrEmpty(manualVersion))
                            {
                                channelToChangeTo = manualVersion;
                            }
                        });
                    }

                    if (string.IsNullOrEmpty(channelToChangeTo))
                    {
                        throw (new Exception("Version required"));
                    }
                    else
                    {
                        if (!channelToChangeTo.IsValidVersion())
                        {
                            throw (new Exception(string.Format("Invalid Version: {0}", channelToChangeTo)));
                        }
                    }

                    await installOffice.ChangeOfficeChannel(channelToChangeTo, baseUrl);

                    Dispatcher.Invoke(() =>
                    {
                        UpdateStatus.Content = "";
                    });
                    
                    var installGenerator = new OfficeInstallManager();
                    LocalInstall = await installGenerator.CheckForOfficeInstallAsync();
                    if (LocalInstall.Installed)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            VersionLabel.Content = LocalInstall.Version;
                            ChannelLabel.Content = LocalInstall.Channel;
                            ProductBranch.SelectedItem = LocalInstall.Channel;
                        });

                        if (LocalInstall.LatestVersionInstalled)
                        {
                            SetItemState(LocalViewItem.Update, LocalViewState.Success);
                        }
                        else
                        {
                            SetItemState(LocalViewItem.Update, LocalViewState.Action);
                            Dispatcher.Invoke(() =>
                            {
                                UpdateStatus.Content = "New version available  (" + LocalInstall.LatestVersion + ")";
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    SetItemState(LocalViewItem.Update, LocalViewState.Fail);
                    Dispatcher.Invoke(() =>
                    {
                        UpdateStatus.Content = "The update failed";
                        ErrorText.Text = ex.Message;
                        RetryButtonColumn.Width = new GridLength(0, GridUnitType.Pixel);
                    });
                    LogErrorMessage(ex);
                }
                finally
                {
                    Dispatcher.Invoke(() =>
                    {
                        ChangeChannel.IsEnabled = true;
                        NewVersion.IsEnabled = true;
                    });
                }
            });
        }
        public async Task RunUpdateOffice()
        {
            await Task.Run(async () =>
            {
                InstallOffice installOffice = null;
                try
                {
                    installOffice = new InstallOffice();
                    installOffice.UpdatingOfficeStatus += installOffice_UpdatingOfficeStatus;

                    Dispatcher.Invoke(() =>
                    {
                        UpdateStatus.Content = "Updating...";
                        ShowVersion.IsEnabled = false;
                        ChangeChannel.IsEnabled = false;
                    });

                    GlobalObjects.ViewModel.BlockNavigation = true;

                    SetItemState(LocalViewItem.Update, LocalViewState.Wait);

                    var currentChannel = LocalInstall.Channel;
                    if (!installOffice.IsUpdateRunning())
                    {
                        var ppDownloader = new ProPlusDownloader();
                        var baseUrl =
                            await ppDownloader.GetChannelBaseUrlAsync(currentChannel, OfficeEdition.Office32Bit);
                        if (string.IsNullOrEmpty(baseUrl))
                            throw (new Exception(string.Format("Cannot find BaseUrl for Channel: {0}", currentChannel)));

                        installOffice.ChangeUpdateSource(baseUrl);
                    }

                    SetSelectedVersion();

                    await installOffice.RunOfficeUpdateAsync(LocalInstall.LatestVersion);
                    
                    Dispatcher.Invoke(() =>
                    {
                        UpdateStatus.Content = "";
                    });

                    var installGenerator = new OfficeInstallManager(); 
                    LocalInstall = await installGenerator.CheckForOfficeInstallAsync();
                    if (LocalInstall.Installed)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            VersionLabel.Content = LocalInstall.Version;
                            ChannelLabel.Content = LocalInstall.Channel;
                            ProductBranch.SelectedItem = LocalInstall.Channel;
                        });

                        if (LocalInstall.LatestVersionInstalled)
                        {
                            SetItemState(LocalViewItem.Update, LocalViewState.Success);
                        }
                        else
                        {
                            SetItemState(LocalViewItem.Update, LocalViewState.Action);
                            Dispatcher.Invoke(() =>
                            {
                                UpdateStatus.Content = "New version available  (" + LocalInstall.LatestVersion + ")";
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    SetItemState(LocalViewItem.Update, LocalViewState.Fail);
                    Dispatcher.Invoke(() =>
                    {
                        UpdateStatus.Content = "The update failed";
                        ErrorText.Text = ex.Message;
                    });

                    LogErrorMessage(ex);
                }
                finally
                {
                    installOffice.ResetUpdateSource();
                    Dispatcher.Invoke(() =>
                    {
                        ShowVersion.IsEnabled = true;
                        ChangeChannel.IsEnabled = true;
                    });
                    GlobalObjects.ViewModel.BlockNavigation = false;
                }
            });
        }
        private async Task LoadViewState()
        {
            try
            {
                await Retry.BlockAsync(10, 1, async () => {
                    Dispatcher.Invoke(() =>
                    {
                       ErrorRow.Visibility = Visibility.Collapsed;
                    });

                    SetItemState(LocalViewItem.Install, LocalViewState.Default);

                    var installGenerator = new OfficeLocalInstallManager(); 
                    LocalInstall = await installGenerator.CheckForOfficeInstallAsync();

                    if (LocalInstall.Installed)
                    {
                        SetItemState(LocalViewItem.Install, LocalViewState.Success);
                        SetItemState(LocalViewItem.Uninstall, LocalViewState.Action);

                        Dispatcher.Invoke(() =>
                        {
                            VersionLabel.Content = "(" + LocalInstall.Version + ")";
                            ChannelLabel.Content = LocalInstall.Channel;

                           // ChannelLabel.Content = "First Release Deferred";

                            if (ChannelLabel.Content.ToString().Length < 10)
                            {
                                ChannelName.Width = new GridLength(100);
                            }
                            else if (ChannelLabel.Content.ToString().Length < 20)
                            {
                                ChannelName.Width = new GridLength(150);
                            }
                            else 
                            {
                                ChannelName.Width = new GridLength(200);
                            }

                            var selectIndex = 0;
                            if (LocalInstall.Channel != null)
                            {
                                for (var i = 0; i < ProductBranch.Items.Count; i++)
                                {
                                    var item = (OfficeBranch) ProductBranch.Items[i];
                                    if (item == null) continue;
                                    if (item.NewName.ToLower() != LocalInstall.Channel.ToLower()) continue;
                                    selectIndex = i;
                                    break;
                                }
                            }

                            BranchChanged(this, new BranchChangedEventArgs()
                            {
                                BranchName = LocalInstall.Channel
                            });

                            ProductBranch.SelectedIndex = selectIndex;
                        });

                        var installOffice = new InstallOffice();
                        if (installOffice.IsUpdateRunning())
                        {
                            await RunUpdateOffice();
                        }
                        else
                        {
                            if (LocalInstall.LatestVersionInstalled)
                            {
                                SetItemState(LocalViewItem.Update, LocalViewState.Success);
                            }
                            else
                            {
                                SetItemState(LocalViewItem.Update, LocalViewState.Action);
                                Dispatcher.Invoke(() =>
                                {
                                    UpdateStatus.Content = "New version available  (" + LocalInstall.LatestVersion + ")";
                                });
                            }
                        }

                        Dispatcher.Invoke(() =>
                        {
                            ChangeChannel.IsEnabled = false;
                        });
                    }
                    else
                    {
                        SetItemState(LocalViewItem.Install, LocalViewState.Action);
                    }
                });
            }
            catch (Exception ex)
            {
                SetItemState(LocalViewItem.Install, LocalViewState.Fail);
                Dispatcher.Invoke(() =>
                {
                    ErrorText.Text = ex.Message;
                });
                LogErrorMessage(ex);
            }
        }