【发布时间】:2014-06-11 10:25:41
【问题描述】:
我有一个使用 .cab 文件进行安装的 Windows CE 6.0 程序。它包含来自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 文件?
【问题讨论】:
标签: c# .net-3.5 windows-ce auto-update cab