用C#做了个在线更新的程序,正常安装时可以使用,断点续传时出错
用C#做了个在线更新的程序,正常安装时可以正常更新软件,
但是断点续传时出错 提示无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集
在wince下双击该程序,提示该程序不是正常的WINCE程序
代码如下:
Application.DoEvents();
float percent = 0;
long totalBytes = 0;
try
{
utils.writelogs("start");
HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(ServerURL);
HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
totalBytes = myrp.ContentLength;
utils.writelogs(totalBytes.ToString());
if (prog != null)
{
prog.Maximum = (int)totalBytes;
}
string sPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
long totalDownloadedByte=0;
long lStartPos = 0;
System.IO.FileStream fs;
Stream st = myrp.GetResponseStream();
if (File.Exists(sPath + "\\updatefile.CAB"))
{
fs = new FileStream(sPath + "\\updatefile.CAB", FileMode.Open, FileAccess.ReadWrite);//File.OpenWrite(StrFileName);
lStartPos = fs.Length;
fs.Seek(lStartPos, SeekOrigin.Current); //移动文件流中的当前指针
//fs = System.IO.File.OpenWrite(sPath + "\\updatefile.CAB");
//lStartPos = fs.Length;
//fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
// so = new FileStream(sPath + "\\updatefile.CAB", FileMode.Append);
//totalDownloadedByte = so.Length;
utils.writelogs("断点续传从该地址开始:" + lStartPos.ToString());
}else{
fs = new FileStream(sPath + "\\updatefile.CAB", FileMode.Create);
//long totalDownloadedByte = 0;
lStartPos = 0;
//int osize1 = so.Read(by, 0, (int)by.Length);
}
utils.writelogs("istartpos:" + lStartPos);
byte[] by = new byte[1024*200];
int osize = st.Read(by, 0, (int)by.Length);
utils.writelogs("osize"+osize.ToString());
while (osize > 0)
{
lStartPos = osize + lStartPos;
//System.Windows.Forms.Application.DoEvents();
fs.Write(by, 0, osize);
if (prog != null)
{
prog.Value = (int)lStartPos;
}
osize = st.Read(by, 0, (int)by.Length);
percent = (float)lStartPos / (float)totalBytes * 100;
int m = percent.ToString().IndexOf(".");
if (m > 0)
{
this.label1.Text = "当前下载进度" + percent.ToString().Substring(0, m) + "%";
}
else
{
this.label1.Text = "当前下载进度100%";
}
System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
//Thread.Sleep(200);
}
fs.Close();
st.Close();
string sPath9 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
utils.writelogs(sPath9 + "\\updatefile.CAB");
utils.writelogs(sPath9 + "\\updatefile.CAB.cab");
//File.Move(sPath9 + "\\updatefile.CAB", sPath9 + "\\updatefile.CAB.cab");
utils.writelogs("写入成功");
}
catch (System.Exception ex)
{
utils.writelogs(ex.Message);
}
[解决办法]
Wince平台与Windos平台还是有区别的。有些写法不支持。请仔细确认