Windows.Management.Update 命名空間
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
Windows.Management.Update 命名空間提供管理跨 Windows 裝置的 Windows Update 操作的 API。 它有幾個不同的用途:
- 一般更新管理:使用像 WindowsUpdateManager、WindowsUpdateAdministrator 和 WindowsUpdate 這類類別,來發現、下載、安裝並監控 Windows 桌面、伺服器及物聯網裝置上的更新,涵蓋預覽版與非預覽版建置。
- Windows 軟體更新提供者平台:使用 WindowsSoftwareUpdateProvider、WindowsSoftwareUpdate、WindowsSoftwareUpdateProviderStatus 以及 WindowsUpdateManager 的軟體更新成員,為你的應用程式撰寫並主機更新提供者,這些更新工具能與 Windows Update Orchestrator 整合。 提供者會在掃描過程中發現特定軟體的更新,然後在協調器排程時執行(下載、安裝、部署、重新啟動)——重複使用與 Windows Update 相同的排程、重啟協調與政策。
- IoT Insider Preview enablement:使用 PreviewBuildsManager 和 PreviewBuildsState 來選擇物聯網裝置進入 Windows Insider 預覽版本並取得註冊狀態。 預期裝置的 OEM 會在 Windows 裝置入口網站建立一個設定頁面,用於裝置設定屬性。
這些 API 群組共存於相同命名空間,但針對不同的情境。 根據你的裝置類型和更新需求,選擇正確的組合。
類別
列舉
範例
掃描並列舉適用更新
這個 C# 範例會建立 WindowsUpdateManager,啟動掃描,並在掃描完成後列舉適用的更新。
using System;
using Windows.Management.Update;
var updateManager = new WindowsUpdateManager("MyClientId");
updateManager.ScanCompleted += (manager, args) =>
{
foreach (WindowsUpdate update in manager.GetApplicableUpdates())
{
Console.WriteLine($"Applicable update: {update.Title}");
}
};
// Scan as if the user selected "Check for updates".
updateManager.StartScan(userInitiated: true);
Author 一個軟體更新提供者
Windows 軟體更新提供者平台允許你將自訂更新來源插入 Windows Update Orchestrator。 每次操作在 SYSTEM 情境中呼叫一次提供者:掃描 過程中會建立WindowsSoftwareUpdate 物件並提交;在 執行動作 時,會透過 WindowsSoftwareUpdateProviderStatus 回報進度與最終結果。
以下片段展示了 C#、C++/WinRT 和 PowerShell 中的核心流程。 完整且可建置的提供者範例——包括 provider.json 清單、程式碼簽署及註冊步驟——會發布在 microsoft/windows-uop 倉庫中。
C#
using System.Collections.Generic;
using Windows.Management.Update;
// Scan: report the updates this provider offers.
void PerformScan(string providerId, IEnumerable<WindowsSoftwareUpdate> updates)
{
var status = new WindowsSoftwareUpdateProviderStatus(providerId);
WindowsSoftwareUpdateResult result = status.SetScanResult(true, 0, 0, updates);
}
// Action: report progress, then the final result.
void PerformAction(string providerId)
{
var status = new WindowsSoftwareUpdateProviderStatus(providerId);
for (ulong i = 0; i <= 100; i += 10)
{
status.SetActionProgress(i, 100);
}
var actionResult = new WindowsSoftwareUpdateProviderActionResult(
WindowsSoftwareUpdateActionResult.Succeeded,
WindowsSoftwareUpdateRestartReason.None,
resultCode: 0,
extendedError: 0);
status.SetActionResult(actionResult);
}
C++/WinRT
#include <winrt/Windows.Management.Update.h>
#include <winrt/Windows.Foundation.Collections.h>
using namespace winrt::Windows::Management::Update;
using namespace winrt::Windows::Foundation::Collections;
// Scan: report the updates this provider offers.
void PerformScan(winrt::hstring const& providerId, IVector<WindowsSoftwareUpdate> const& updates)
{
WindowsSoftwareUpdateProviderStatus status{ providerId };
auto result = status.SetScanResult(true, S_OK, 0, updates);
}
// Action: report progress, then the final result.
void PerformAction(winrt::hstring const& providerId)
{
WindowsSoftwareUpdateProviderStatus status{ providerId };
for (uint64_t i = 0; i <= 100; i += 10)
{
status.SetActionProgress(i, 100);
}
WindowsSoftwareUpdateProviderActionResult actionResult{
WindowsSoftwareUpdateActionResult::Succeeded,
WindowsSoftwareUpdateRestartReason::None,
0, // resultCode
0 }; // extendedError
status.SetActionResult(actionResult);
}
PowerShell
# Scan: report the updates this provider offers.
$providerStatus = [Windows.Management.Update.WindowsSoftwareUpdateProviderStatus]::new($ProviderId)
$statusResult = $providerStatus.SetScanResult($true, 0, 0, $UpdateCollection)
# Action: report progress, then the final result.
$providerStatus = [Windows.Management.Update.WindowsSoftwareUpdateProviderStatus]::new($ProviderId)
for ($i = 0; $i -le 100; $i += 10) {
[void]$providerStatus.SetActionProgress($i, 100)
}
$actionResult = [Windows.Management.Update.WindowsSoftwareUpdateProviderActionResult]::new(
[Windows.Management.Update.WindowsSoftwareUpdateActionResult]::Succeeded,
[Windows.Management.Update.WindowsSoftwareUpdateRestartReason]::None,
0, # resultCode
0) # extendedError
[void]$providerStatus.SetActionResult($actionResult)
Determine Insider Preview build status (IoT)
這個 C# 範例展示了使用者如何判斷裝置是否收到預覽建置,或是否存在問題阻止裝置取得預覽建置。
public string GetErrorMessage()
{
PreviewBuildsManager manager = PreviewBuildsManager.GetDefault();
if (manager != null)
{
PreviewBuildsState state = manager.GetCurrentState();
if (state.Properties.ContainsKey("ErrorMessage"))
{
return state.Properties["ErrorMessage"].ToString();
}
}
return String.Empty;
}
備註
- 在所有支援平台上,使用 WindowsUpdateManager、 WindowsUpdate 和 WindowsUpdateAdministrator 來進行一般更新管理。
- 請使用 WindowsSoftwareUpdateProvider、WindowsSoftwareUpdateStatus、WindowsSoftwareUpdate 以及 WindowsUpdateManager 的軟體更新成員,為你的應用程式(Windows Update Orchestration Platform)撰寫並主機更新提供者。 關於提供者撰寫、導入、簽署與註冊的指引,以及完整的 C#、C++ 和 PowerShell 範例,請參閱 microsoft/windows-uop 儲存庫。
- PreviewBuildsManager 和 PreviewBuildsState 是 物聯網專用 的,無法管理桌面或伺服器上的一般更新。 預期裝置的 OEM 會在 Windows 裝置入口網站建立一個設定頁面,用於裝置設定屬性。 PreviewBuildsState 的資訊會被裝置讀取,然後顯示給使用者,然後用來註冊使用者的帳號到裝置。 註冊完成後,所有設定管理將透過 Windows 測試人員計畫 線上完成,而非裝置本身。
- 平台可用性與 API 合約會記錄在各個類別的參考頁面中。
| API 集合 | 支援的 Windows 版本 |
|---|---|
| 預覽版本 | 17134年或更久 |
| Windows Update 管理員 | 22621 或更大 |
| Windows Update 管理員 | 22621 或更大 |
| Windows 軟體更新 | 26100 或更高 |