我有一个Windows CE 6.0程序,它使用.cab文件进行安装 . 它包含http://msdn.microsoft.com/en-us/library/aa446487.aspx的指南,以支持自动自我更新 .
程序可以检查更新就好了,它会按照它应该下载更新,但是当我尝试将它解压缩刚下载的.cab文件时,它会失败 .
这是我的代码:
void ResponseReceived(IAsyncResult res)
{
try
{
_mResp = (HttpWebResponse)_mReq.EndGetResponse(res);
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString(), "Error");
return;
}
// Allocate data buffer
_dataBuffer = new byte[DataBlockSize];
// Set up progrees bar
_maxVal = (int)_mResp.ContentLength;
pgbDownloadBar.Invoke(new EventHandler(SetProgressMax));
// Open file stream to save received data
_mFs = new FileStream(@"\Application\CCOptimizerSetup.cab",
FileMode.Create);
// Request the first chunk
_mResp.GetResponseStream().BeginRead(_dataBuffer, 0, DataBlockSize,
OnDataRead, this);
}
void OnDataRead(IAsyncResult res)
{
// How many bytes did we get this time
int nBytes = _mResp.GetResponseStream().EndRead(res);
// Write buffer
_mFs.Write(_dataBuffer, 0, nBytes);
// Update progress bar using Invoke()
_pbVal += nBytes;
pgbDownloadBar.Invoke(new EventHandler(UpdateProgressValue));
// Are we done yet?
if (nBytes > 0)
{
// No, keep reading
_mResp.GetResponseStream().BeginRead(_dataBuffer, 0,
DataBlockSize, OnDataRead, this);
}
else
{
// Yes, perform cleanup and update UI.
_mFs.Close();
_mFs = null;
Invoke(new EventHandler(AllDone));
}
}
private void AllDone(object sender, EventArgs e)
{
Cursor.Current = Cursors.Default;
DialogResult dialogresult = MessageBox.Show(@"CCOptimizer has finished downloading. Open now?", "Download complete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
switch (dialogresult)
{
case DialogResult.OK:
Application.Exit();
Dispose();
Close();
Invoke(new EventHandler(StartProcess));
break;
case DialogResult.Cancel:
Form1 oForm = new Form1();
oForm.Show();
Hide();
break;
}
}
private void StartProcess(object sender, EventArgs e)
{
Process.Start(@"\Application\CCOptimizerSetup.cab", null);
}
它不起作用 . 无论我在哪里放置 StartProcess()
的调用,要么只是关闭而没有消息,要么在解压缩过程中出现错误,我正在尝试更新的其中一个文件仍在使用中 . 在调试过程中,它只是停止程序 . 如果我尝试安装的版本,它会非常短暂地闪烁一个窗口,然后在WaitCursor旋转的情况下锁定机器并需要重新启动,尽管之后它可以安装得很好 .
如何关闭当前程序并打开.cab文件而无需重新启动计算机?
1 回答
您的更新逻辑中存在缺陷,可能是您要更新的应用程序是检查更新的事情(问题并不完全清楚,但行为表明情况就是这样) .
应用程序无法直接更新自身,因为对于应该相当明显的原因,您无法替换已加载和运行的程序集 .
一般来说,有两种方法可以解决这个问题 . 两者都需要第二个可执行文件(在很多情况下,我已经拥有了一个看门狗应用程序) .
而不是直接启动您的应用程序,首先启动一些"updater"应用程序 . 该应用程序可以检查更新,通知用户,下载并安装/覆盖目标应用程序,因为它已完成,或者如果不需要更新步骤 .
不是直接启动您的应用程序,而是首先启动一些"bootstrap"应用程序 . 引导程序在本地临时存储中查找任何更新文件,如果存在则执行它们,然后运行正常的应用程序 . 让应用程序自己检查更新并将它们拉到临时存储位置 . 拔出更新后,将其关闭并重新启动引导程序,然后将应用更新 .