c# 软件自动在线更新代码.docx_第1页
c# 软件自动在线更新代码.docx_第2页
c# 软件自动在线更新代码.docx_第3页
c# 软件自动在线更新代码.docx_第4页
c# 软件自动在线更新代码.docx_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

程序部份代码参考自:/hjs1573/archive/2007/09/11/889826.html服务器空间为.Net 2.0空间注意:网盘失效,源码无法下载,不过所有的源代码都已经贴出来了,可以自己新建一个工程,然后把代码复制进去,自行编译服务器空间文件列表:/UpdateSize.ashx/AutoUpdater/此文件夹放更新文件/AutoUpdater/AutoUpdater.xmlUpdateSize.ashx:123usingSystem;4usingSystem.Web;5usingSystem.IO;67publicclassUpdateSize:IHttpHandler89publicvoidProcessRequest(HttpContextcontext)10stringdirPath=context.Server.MapPath(/AutoUpdater/);11context.Response.ContentType=text/xml;12context.Response.Expires=-1;13context.Response.Write();14context.Response.Write();15context.Response.End();161718/19/获取所有下载文件大小20/21/返回值22privatestaticlongGetUpdateSize(stringdirPath)2324/判断文件夹是否存在,不存在则退出25if(!Directory.Exists(dirPath)26return0;27longlen;28len=0;29DirectoryInfodi=newDirectoryInfo(dirPath);30/获取所有文件大小31foreach(FileInfofiindi.GetFiles()3233/剔除升级数据文件34if(fi.Name!=AutoUpdater.xml)35len+=fi.Length;3637returnlen;383940publicboolIsReusable41get42returnfalse;434445AutoUpdater.xml:Maxthon207.exeMaxthon207.exe客户端conf.config:AutoUpdater.cs:1usingSystem;2usingSystem.ComponentModel;3usingSystem.Data;4usingSystem.Globalization;5usingSystem.IO;6usingSystem.Net;7usingSystem.Text;8usingSystem.Windows.Forms;9usingSystem.Xml;1011namespaceYesuo1213publicpartialclassAutoUpdater:Form1415privateWebClientdownWebClient=newWebClient();16privatestaticstringdirPath;17privatestaticlongsize;/所有文件大小18privatestaticintcount;/文件总数19privatestaticstringfileNames;20privatestaticintnum;/已更新文件数21privatestaticlongupsize;/已更新文件大小22privatestaticstringfileName;/当前文件名23privatestaticlongfilesize;/当前文件大小2425publicAutoUpdater()2627InitializeComponent();282930privatevoidComCirUpdate_Load(objectsender,EventArgse)3132dirPath=GetConfigValue(conf.config,Url);33stringthePreUpdateDate=GetTheLastUpdateTime(dirPath);34stringlocalUpDate=GetConfigValue(conf.config,UpDate);35if(!String.IsNullOrEmpty(thePreUpdateDate)&!String.IsNullOrEmpty(localUpDate)3637if(DateTime.Compare(38Convert.ToDateTime(thePreUpdateDate,CultureInfo.InvariantCulture),39Convert.ToDateTime(localUpDate,CultureInfo.InvariantCulture)0)4041UpdaterStart();4243else4445UpdaterClose();464748else4950UpdaterClose();5152/UpdaterStart();535455/56/开始更新57/58privatevoidUpdaterStart()5960floattempf;61/委托下载数据时事件62this.downWebClient.DownloadProgressChanged+=delegate(objectwcsender,DownloadProgressChangedEventArgsex)6364this.label2.Text=String.Format(65CultureInfo.InvariantCulture,66正在下载:01/2,67fileName,68ConvertSize(ex.BytesReceived),69ConvertSize(ex.TotalBytesToReceive);7071filesize=ex.TotalBytesToReceive;72tempf=(float)(upsize+ex.BytesReceived)/size);73gressBar1.Value=Convert.ToInt32(tempf*100);74gressBar2.Value=ex.ProgressPercentage;75;76/委托下载完成时事件77this.downWebClient.DownloadFileCompleted+=delegate(objectwcsender,AsyncCompletedEventArgsex)7879if(ex.Error!=null)8081MeBox(ex.Error.Message);8283else8485if(File.Exists(Application.StartupPath+fileName)8687File.Delete(Application.StartupPath+fileName);8889File.Move(Application.StartupPath+AutoUpdater+fileName,Application.StartupPath+fileName);90upsize+=filesize;91if(fileNames.Lengthnum)9293DownloadFile(num);9495else9697SetConfigValue(conf.config,UpDate,GetTheLastUpdateTime(dirPath);98UpdaterClose();99100101;102103size=GetUpdateSize(dirPath+UpdateSize.ashx);104if(size=0)105UpdaterClose();106num=0;107upsize=0;108UpdateList();109if(fileNames!=null)110DownloadFile(0);111112113/114/获取更新文件大小统计115/116/更新文件数据XML117/返回值118privatestaticlongGetUpdateSize(stringfilePath)119120longlen;121len=0;122try123124WebClientwc=newWebClient();125Streamsm=wc.OpenRead(filePath);126XmlTextReaderxr=newXmlTextReader(sm);127while(xr.Read()128129if(xr.Name=UpdateSize)130131len=Convert.ToInt64(xr.GetAttribute(Size),CultureInfo.InvariantCulture);132break;133134135xr.Close();136sm.Close();137138catch(WebExceptionex)139140MeBox(ex.Message);141142returnlen;143144145/146/获取文件列表并下载147/148privatestaticvoidUpdateList()149150stringxmlPath=dirPath+AutoUpdater/AutoUpdater.xml;151WebClientwc=newWebClient();152DataSetds=newDataSet();153ds.Locale=CultureInfo.InvariantCulture;154155try156157Streamsm=wc.OpenRead(xmlPath);158ds.ReadXml(sm);159DataTabledt=ds.TablesUpdateFileList;160StringBuildersb=newStringBuilder();161count=dt.Rows.Count;162for(inti=0;idt.Rows.Count;i+)163164if(i=0)165166sb.Append(dt.RowsiUpdateFile.ToString();167168else169170sb.Append(,+dt.RowsiUpdateFile.ToString();171172173fileNames=sb.ToString().Split(,);174sm.Close();175176catch(WebExceptionex)177178MeBox(ex.Message);179180181182/183/下载文件184/185/下载序号186privatevoidDownloadFile(intarry)187188try189190num+;191fileName=fileNamesarry;192this.label1.Text=String.Format(193CultureInfo.InvariantCulture,194更新进度0/12,195num,196count,197ConvertSize(size);198199gressBar2.Value=0;200this.downWebClient.DownloadFileAsync(201newUri(dirPath+AutoUpdater/+fileName),202Application.StartupPath+AutoUpdater+fileName);203204catch(WebExceptionex)205206MeBox(ex.Message);207208209210/211/转换字节大小212/213/输入字节数214/返回值215privatestaticstringConvertSize(longbyteSize)216217stringstr=;218floattempf=(float)byteSize;219if(tempf/10241)220221if(tempf/1024)/10241)222223str=(tempf/1024)/1024).ToString(#0.00,CultureInfo.InvariantCulture)+MB;224225else226227str=(tempf/1024).ToString(#0.00,CultureInfo.InvariantCulture)+KB;228229230else231232str=tempf.ToString(CultureInfo.InvariantCulture)+B;233234returnstr;235236237/238/弹出提示框239/240/输入提示信息241privatestaticvoidMeBox(stringtxt)242243MessageBox.Show(244txt,245提示信息,246MessageBoxButtons.OK,247MessageBoxIcon.Asterisk,248MessageBoxDefaultButton.Button1,249MessageBoxOptions.DefaultDesktopOnly);250251252/253/关闭程序254/255privatestaticvoidUpdaterClose()256257try258259System.Diagnostics.Process.Start(Application.StartupPath+ComCir.exe);260261catch(Win32Exceptionex)262263MeBox(ex.Message);264265Application.Exit();266267268/269/读取.exe.config的值270/271/.exe.config文件的路径272/key的值273/返回value的值274internalstaticstringGetConfigValue(stringpath,stringappKey)275276XmlDocumentxDoc=newXmlDocument();277XmlNodexNode;278XmlElementxElem=null;279try280281xDoc.Load(path);282283xNode=xDoc.SelectSingleNode(/appSettings);284285xElem=(XmlElement)xNode.SelectSingleNode(/addkey=+appKey+);286287288catch(XmlExceptionex)289290MeBox(ex.Message);291292if(xElem!=null)293returnxElem.GetAttribute(value);294else295return;296297298/299/设置.exe.config的值300/301/.exe.config文件的路径302/key的值303/value的值304internalstaticvoidSetConfigValue(stringpath,stringappKey,stringappValue)305306XmlDocumentxDoc=newXmlDocument();307try308309xDoc.Load(path);310311XmlNodexNode;312XmlElementxElem1;313XmlElementxElem2;314315xNode=xDoc.SelectSingleNode(/appSettings);316317xElem1=(XmlElement)xNode.SelectSingleNode(/addkey=+appKey+);318if(xElem1!=null)xElem1.SetAttribute(value,appValue);319else320321xElem2=xDoc.CreateEleme

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论