如果没有可下载或安装的更新,如何退出C#控制台应用程序?
我正忙着编写一个控制台应用程序,它在用户pc上搜索任何更新,然后下载并安装。所有这些都很好,但是当没有可用的更新时,它会尝试下载,然后应用程序因此错误崩溃如果没有可下载或安装的更新,如何退出C#控制台应用程序?,c#,console,C#,Console,我正忙着编写一个控制台应用程序,它在用户pc上搜索任何更新,然后下载并安装。所有这些都很好,但是当没有可用的更新时,它会尝试下载,然后应用程序因此错误崩溃 System.Runtime.InteropServices.COMException: '0x80240024' 我怀疑它找不到要下载的更新,因此它抛出了这个COMexception 这是我正在使用的代码 public static UpdateCollection DownloadUpdates() { Upda
System.Runtime.InteropServices.COMException: '0x80240024'
我怀疑它找不到要下载的更新,因此它抛出了这个COMexception
这是我正在使用的代码
public static UpdateCollection DownloadUpdates()
{
UpdateSession UpdateSession = new UpdateSession();
IUpdateSearcher SearchUpdates = UpdateSession.CreateUpdateSearcher();
ISearchResult UpdateSearchResult = SearchUpdates.Search("IsInstalled=0 and IsPresent=0");
UpdateCollection UpdateCollection = new UpdateCollection();
//Accept Eula code for each update
for (int i = 0; i < UpdateSearchResult.Updates.Count; i++)
{
IUpdate Updates = UpdateSearchResult.Updates[i];
if (Updates.EulaAccepted == false)
{
Updates.AcceptEula();
}
UpdateCollection.Add(Updates);
}
//Accept Eula ends here
//if it is zero i am not sure if it will trow an exception -- I havent tested it.
UpdateCollection DownloadCollection = new UpdateCollection();
UpdateDownloader Downloader = UpdateSession.CreateUpdateDownloader();
for (int i = 0; i < UpdateCollection.Count; i++)
{
DownloadCollection.Add(UpdateCollection[i]);
}
Downloader.Updates = DownloadCollection;
Console.WriteLine("Downloading Updates");
IDownloadResult DownloadResult = Downloader.Download(); ---> Exception Thrown here!!
UpdateCollection InstallCollection = new UpdateCollection();
for (int i = 0; i < UpdateCollection.Count; i++)
{
if (DownloadCollection[i].IsDownloaded)
{
InstallCollection.Add(DownloadCollection[i]);
}
}
return InstallCollection;
}
public static void InstallUpdates(UpdateCollection DownloadedUpdates)
{
UpdateSession UpdateSession = new UpdateSession();
UpdateInstaller InstallAgent = UpdateSession.CreateUpdateInstaller() as UpdateInstaller;
InstallAgent.Updates = DownloadedUpdates;
//Starts a synchronous installation of the updates.
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa386491(v=VS.85).aspx#methods
IInstallationResult InstallResult = InstallAgent.Install();
}
public static void EnableUpdateServices()
{
IAutomaticUpdates updates = new AutomaticUpdates();
if (!updates.ServiceEnabled)
{
Console.WriteLine("Not all updates services where enabled. Enabling Now" + updates.ServiceEnabled);
updates.EnableService();
Console.WriteLine("Service enable success");
}
}
}
}
我尝试添加了一个try catch,但没有成功
我只需要告诉它,当没有可用的更新时,可以说“没有可用的更新,请按任意键退出”
谢谢“我尝试添加了一个try-catch,但没有工作。”如果没有可用的更新,
UpdateCollection.Count
?@Cid不确定,它应该是0,因为没有可用的更新,您可以检查它吗?然后,您可以通过一个简单的if/else
IDownloadResult DownloadResult = Downloader.Download();