/// <summary>
        /// Creates a <see cref="WuApiController"/> which uses the given Interfaces to search, download and install updates.
        /// </summary>
        /// <param name="session">Session to be used.</param>
        /// <param name="updateCollectionFactory">Factory to create <see cref="IUpdateCollection"/>s.</param>
        /// <param name="systemInfo">System informations about the OS enviroment.</param>
        public WuApiController(IUpdateSession session, UpdateCollectionFactory updateCollectionFactory, ISystemInfo systemInfo)
        {
            if (session == null)
            {
                throw new ArgumentNullException(nameof(session));
            }
            if (updateCollectionFactory == null)
            {
                throw new ArgumentNullException(nameof(updateCollectionFactory));
            }
            if (systemInfo == null)
            {
                throw new ArgumentNullException(nameof(systemInfo));
            }

            UpdateSession = session;
            UpdateSession.ClientApplicationID = this.GetType().FullName;
            UpdateSearcher          = session.CreateUpdateSearcher();
            UpdateDownloader        = session.CreateUpdateDownloader();
            UpdateInstaller         = session.CreateUpdateInstaller();
            UpdateCollectionFactory = updateCollectionFactory;
            SystemInfo       = systemInfo;
            StateTransitions = SetupStateTransitions();

            EnterState((SystemInfo.IsRebootRequired()) ? (WuProcessState) new WuStateRebootRequired() : new WuStateReady());
            Log.Info("Initial state: " + _currentState.GetType().Name);
            if (Log.IsDebugEnabled)
            {
                OnStateChanged            += (s, e) => { Log.Debug($"Event {nameof(OnStateChanged)} fired: {e.ToString()}"); };
                OnAsyncOperationCompleted += (s, e) => { Log.Debug($"Event {nameof(OnAsyncOperationCompleted)} fired: {e.ToString()}"); };
                OnProgressChanged         += (s, e) => { Log.Debug($"Event {nameof(OnProgressChanged)} fired: {e.ToString()}"); };
            }
        }
Beispiel #2
0
        public WindowsUpdateAgent()
        {
            this.mUpdateSearcher = this.mUpdateSession.CreateUpdateSearcher();
            int totalHistoryCount = this.mUpdateSearcher.GetTotalHistoryCount();

            if (totalHistoryCount > 0)
            {
                this.mUpdateHistoryCollection = this.mUpdateSearcher.QueryHistory(0, totalHistoryCount);
                Regex regex = new Regex(@"KB\d{5,5}");
                foreach (IUpdateHistoryEntry entry in this.mUpdateHistoryCollection)
                {
                    Match match = regex.Match(entry.Title);
                    if (match.Success)
                    {
                        if (this.mInstalledUpdates.ContainsKey(match.Value))
                        {
                            if (this.mInstalledUpdates[match.Value].RevisionNumber < Convert.ToInt32(entry.UpdateIdentity.RevisionNumber))
                            {
                                this.mInstalledUpdates[match.Value] = new WindowsUpdate(match.Value, entry.Title, new Guid(entry.UpdateIdentity.UpdateID), Convert.ToInt32(entry.UpdateIdentity.RevisionNumber));
                            }
                        }
                        else
                        {
                            this.mInstalledUpdates.Add(match.Value, new WindowsUpdate(match.Value, entry.Title, new Guid(entry.UpdateIdentity.UpdateID), Convert.ToInt32(entry.UpdateIdentity.RevisionNumber)));
                        }
                    }
                }
                regex = null;
                this.mUpdateHistoryCollection = null;
            }
            this.mUpdateSearcher = null;
            this.mUpdateSession  = null;
        }
        public WindowsOperationHandler()
        {
            // Have to make sure the Windows Update Agent is up to date.
            WUAUpdater.Run();

            session = new UpdateSession();
            windowsUninstaller = new Uninstaller();

            updateWorker.DoWork += new DoWorkEventHandler(DownloadAndInstallUpdates);
            updateWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DownloadAndInstallCompleted);
        }
 public static UpdateDownloader CreateDownloader(this IUpdateSession updateSession)
 {
     return(updateSession.CreateUpdateDownloader());
 }