我使用的是带有 NET 7.0 的 Visual Studio 2022 Professional 版本 17。我有以下问题:当我使用 clickonce 发布新版本时。一旦用户跳过更新,它就不会再次询问。我需要应用程序要求每次运行时更新程序,除非他们有最新版本。
以下不适用于.net7
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);
if (ApplicationDeployment.IsNetworkDeployed)
{
ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
try
{
UpdateCheckInfo info = ad.CheckForDetailedUpdate();
if (info.UpdateAvailable)
{
bool doUpdate = true;
if (!info.IsUpdateRequired)
{
DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
if (DialogResult.OK != dr)
{
doUpdate = false;
}
}
else
{
MessageBox.Show("A mandatory update is available. The application will now update and restart.", "Update Available", MessageBoxButtons.OK);
}
if (doUpdate)
{
ad.Update();
Application.Restart();
}
}
}
catch (DeploymentDownloadException dde)
{
}
catch (InvalidOperationException)
{
}
}
我确实从https://learn.microsoft.com/en-us/visualstudio/deployment/access-clickonce-deployment-properties-dotnet?view=vs-2022找到了这个。但是,仍然不知道如何启动更新过程
if (Environment.GetEnvironmentVariable("ClickOnce_IsNetworkDeployed")?.ToLower() == "true") { 版本 currentVersion = new Version(Environment.GetEnvironmentVariable("ClickOnce_CurrentVersion")); 版本更新版本 = 新版本(Environment.GetEnvironmentVariable("ClickOnce_UpdatedVersion"));
if (updatedVersion > currentVersion)
{
DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
}
}
}
添加哈桑·达纳奇,
如果有新的更新,UpdatedVersion 与 CurrentVersion 不同 已安装,但您尚未在应用程序上调用“重新启动”。如果 应用程序的部署清单配置为执行 自动更新,您可以比较这两个值来确定 是否应该重新启动应用程序。
如果应用程序尚未更新,UpdatedVersion 返回 与当前版本相同的值。
下面提供的解决方案仅在用户在部署更新后重新启动应用程序时才有效。如果您想要更新当前打开的应用程序,它将不起作用。我尚未找到无需重新启动应用程序即可工作的解决方案。
if (Environment.GetEnvironmentVariable("ClickOnce_IsNetworkDeployed")?.ToLower() == "true") {
Version currentVersion = new
Version(Environment.GetEnvironmentVariable("ClickOnce_CurrentVersion"));
Version updatedVersion = new
Version(Environment.GetEnvironmentVariable("ClickOnce_UpdatedVersion"));
if (updatedVersion > currentVersion)
{
DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
}
}