用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) + "%";
}