首页 c 实现软件自动更新思路(The idea of automatic updating software by c )

c 实现软件自动更新思路(The idea of automatic updating software by c )

举报
开通vip

c 实现软件自动更新思路(The idea of automatic updating software by c )c 实现软件自动更新思路(The idea of automatic updating software by c ) c#实现软件自动更新思路(The idea of automatic updating software by c#) Preface For a long time, the use of Client/Server for the majority of programmers are still using the Browser/Server structure, arguing,...

c 实现软件自动更新思路(The idea of automatic updating software by c )
c 实现软件自动更新思路(The idea of automatic updating software by c ) c#实现软件自动更新思路(The idea of automatic updating software by c#) Preface For a long time, the use of Client/Server for the majority of programmers are still using the Browser/Server structure, arguing, in these arguments, the structure of C/S program maintainability, difficult arrangement, the upgrade is not convenient, high maintenance cost is an important factor. There are many enterprise users is because of this reason to abandon the use of C/S. However, when an application must use C/S structure to achieve its function well, how can we solve the client deployment problem with auto upgrade? The deployment is very simple, just click on the software to be installed, difficult is that whenever a new version is released, can automatically upgrade [3]. Now, our goal is very simple, we hope to develop a reusable application independent and can automatically upgrade system. Here I provide a reusable C# prepared by the automatic upgrade system. There are 2 automatic upgrade software implementation difficulties First, in order to find the way to update the remote server, applications must have a network query, the application needs to communicate with the server network programming, simple protocol. Second download. Download look does not need to consider the Internet of things, but to consider the user's request and download files, download large files without user consent. Friendly automatic update applications will use the remaining bandwidth to download the update. It sounds simple, but it is a technical problem, fortunately there is already a solution. The third consideration is the use of the new version of the application program to replace the original application process. This problem is interesting, because it requires the code to run when he removed from the system, there are several ways to achieve the function of [5], this program is mainly implemented to replace the new version of the application function through the comparison of the new and old version date. 3 implementation of the principle of automatic software online upgrade Write two procedures, one is the main program; an upgrade program; all upgrade tasks are completed by the upgrade program. 1. start the upgrade program, upgrade program to connect to the site to download the main program (of course also includes a new XML configuration document support library files, etc.) to a temporary folder; 2. upgrade program to obtain a new version of the program server in the XML configuration file update date or version number or file size; 3. upgrade program to obtain the original client application date of last update or version number or file size comparison; if found to date with the latest upgrade program to date than the original program, the user is prompted whether to upgrade; or using compare with the existing version of the latest version, the latest discovery prompts whether the user upgrades; it was also used by other attributes such as compared to the file size, found upgrade program file size larger than the old version of the size of the program the user is prompted to upgrade. This paper mainly uses the comparison of new and old version of the update date, to prompt the user to upgrade. 4. if users choose to upgrade, then gets the download file list, start batch download documents; 5. upgrade program to detect whether the main program of old activity, if the activity is closed the old main program; 6. remove the old copy of the main program, the temporary files in the folder to the corresponding position; 7. check the main procedures of the state, if the state for the activities, the main program is to start a new; 8. close the upgrade program, the upgrade is complete [4]. 4 key steps to achieve the online upgrade by C# Here I mainly use the date information to determine whether you need to download the upgrade version. 4.1 prepare a XML configuration file The name of AutoUpdater.xml, functions as a template for the upgrade, you need to upgrade information display. The name of AutoUpdater.xml, functions as a template for the upgrade, you need to upgrade information display. "URLAddres URL= ." 113/vbroker/log/ "/ > / / upgrade file where the server. < UpdateInfo > "UpdateTime Date =" 2005-02-02 "/ > / / upgrade file update date "Version Num =" 1.0.0.1 "/ > / / upgrade file version number < /UpdateInfo > < UpdateFileList > / / upgrade file list "UpdateFile FileName =" aa.txt "/ > / / there are three documents required to upgrade "UpdateFile FileName =" VB40.rar "/ > "UpdateFile FileName =" VB4-1.CAB "/ > < /UpdateFileList > < RestartApp > "ReStart Allow =" Yes "/ > / / allowed to restart the application "AppName Name =" TIMS.exe "/ > / / start the application name < /RestartApp > < /AutoUpdater > //xml version number Where the file server / upgrade. / / update date updates / / upgrade file version number / / upgrade file list / / there are three documents required to upgrade Allow / restart the application Start / / application name From the above XML document that can upgrade the server address, the document is the upgrade document update date, file list to be upgraded, including a total of three documents: aa.txt, VB40.rar, VB4-1.CAB upgrade. And if allowed to restart the application and restart the application name. Date of last update 4.2 for the client application and the server upgrade program Through the GetTheLastUpdateTime () function to achieve. Private string GetTheLastUpdateTime (string Dir) { String = LastUpdateTime""; String = Dir + AutoUpdaterFileName @ \AutoUpdater.xml"; If (! File.Exists (AutoUpdaterFileName)) Return LastUpdateTime; / / open XML file FileStream myFile = new FileStream (AutoUpdaterFileName, FileMode.Open); //xml file reader XmlTextReader XML = new XmlTextReader (myFile); While ((xml.Read)) { If (xml.Name = = "UpdateTime") { / / get the last update date upgrade document LastUpdateTime = xml.GetAttribute ("Date"); Break; } } (xml.Close); (myFile.Close); Return LastUpdateTime; } Open the XML document by XmlTextReader, read the update time so as to obtain the corresponding value of Date, which is a recent update time server upgrade file. Function call: The client specifies the application path / / get the last update time String thePreUpdateDate = GetTheLastUpdateTime (Application.StartupPath); Application.StartupPath refers to the path where the client application program. Get / / date of last update document from the server download String theLastsUpdateDate = GetTheLastUpdateTime (theFolder.FullName); TheFolder.FullName refers to the upgrade path to download the document to the temporary folder on the client's. 4.3 date Date of last update to compare the client application date of last update and upgrade of the server program. Get / / date of last update downloaded documents String theLastsUpdateDate = GetTheLastUpdateTime (theFolder.FullName); If (= "thePreUpdateDate!") { / / if the application update date application client will upgrade the server upgrade more than update date If (Convert. Todatetime (thepreupdatedate) > = convert.todatetime (thelastsupdatedate)) { Messagebox.show ("当前软件已经是最新的, 无需更新!", "系统提 示", messageboxbuttons.ok, messageboxicon.information); This.close (); } } This.labdownfile.text = " 下载 课程表模板下载资产负债表下载英语单词下载学习机资料下载励志文章下载 更新文件"; This.labfilename.refresh (); This.btncancel.enabled = true; This.progressbar.position = 0; This.progressbartotal.position = 0; This.progressbartotal.refresh (); This.progressbar.refresh (); / / 通过动态数组获取下载文件的列 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf ArrayList list = getdownfilelist (gettheupdateurl (), thefolder.fullname); String [] url = new string [list.count]; List.copyto (URLs, 0); 将客户端升级的应用程序的日期与服务器端下载的应用程序日期进 行比较, 如果前者大于后者, 则不更新; 如果前者小于后者, 则通 过动态数组获取下载文件的列表, 开始下载文件. 4.2 获取客户端应用程序及服务器端升级程序的最近一次更新日期 通过getthelastupdatetime () 函数来实现. Private string getthelastupdatetime (string DIR) { Lastupdatetime string = ""; String autoupdaterfilename = dir + @ "\ autoupdater.xml"; If (! File.exists (autoupdaterfilename)) Return lastupdatetime; / / 打开xml文件 FileStream myfile = new FileStream (autoupdaterfilename, filemode.open); / / xml文件阅读器 XMLTextReader XML = new XMLTextReader (myfile); While (xml.read ()) { If (xml.name = = "Updatetime") { / / 获取升级文档的最后一次更新日期 Lastupdatetime = xml.getattribute ("date"); Break; } } Xml.close (); Myfile.close (); Return lastupdatetime; } 通过xmltextreader打开xml文档, 读取更新时间从而获取date对 应的值, 即服务器端升级文件的最近一次更新时间. 函数调用实现: / / 获取客户端指定路径下的应用程序最近一次更新时间 String thepreupdatedate = getthelastupdatetime (application.startuppath); Application.startuppath指客户端应用程序所在的路径. / / 获得从服务器端已下载文档的最近一次更新日期 String thelastsupdatedate = getthelastupdatetime (thefolder.fullname); Thefolder.fullname指在升级文档下载到客户机上的临时文件夹所 在的路径. 4.3 比较日期 客户端应用程序最近一次更新日期与服务器端升级程序的最近一次 更新日期进行比较. / / 获得已下载文档的最近一次更新日期 String thelastsupdatedate = getthelastupdatetime (thefolder.fullname); If (thepreupdatedate! = "") { / / 如果客户端将升级的应用程序的更新日期大于服务器端升级的应用程序的更新日期 If (convert.todatetime (thepreupdatedate) > = convert.todatetime (thelastsupdatedate)) { Messagebox.show ("当前软件已经是最新的, 无需更新!", "系统提示", messageboxbuttons.ok, messageboxicon. 信息); 这(); } } this.labdownfile.text =“下载更新文件”; 这refresh()。labfilename; this.btncancel.enabled =真; this.progressbar.position = 0; this.progressbartotal.position = 0; 这refresh()。progressbartotal; 这refresh()。进度条; / /通过动态数组获取下载文件的列表 ArrayList列表= getdownfilelist(gettheupdateurl(),文件夹。全名); [网址=新的字符串,字符串] [数]列表; 名单。CopyTo(URL,0); 将客户端升级的应用程序的日期与服务器端下载的应用程序日期进行比较,如果前者大于后者,则不更新;如果前者小于后者,则通过动态数组获取下载文件的列表,开始下载文件。 通过batchdownload()函数来实现。升级程序检测旧的主程序是否活动,若活动则关闭旧的主程序;删除旧的主程序,拷贝临时文件夹中的文件到相应的位置;检查主程序的状态,若状态为活动的,则启动新的主程序。 private void BatchDownload(对象) { 这个调用(this.activestatechanger,新对象[ ] {真、假}); 尝试 { downloadinstructions指令=(downloadinstructions)数据; / /批量下载 使用(batchdownloader BDL =新batchdownloader()) { bdl.currentprogresschanged + =新DownloadProgressHandler(这。singleprogresschanged); bdl.statechanged + =新DownloadProgressHandler(这。statechanged); bdl.filechanged + =新DownloadProgressHandler(bdl_filechanged); bdl.totalprogresschanged + =新DownloadProgressHandler (bdl_totalprogresschanged); BDL。下载(instructions.urls,指示目的地,(manualresetevent),cancelevent); } } catch(Exception ex) { showerrormessage(前); } 这个调用(this.activestatechanger,新对象[ ] {假,假}); this.labfilename.text =“”; / /更新程序 如果(这。_update) { / /关闭原有的应用程序 this.labdownfile.text =“正在关闭程序的„”; 系统诊断过程[ ] proc =系统。诊断过程。GetProcessesByName(“时间”); / /关闭原有应用程序的所有进程 foreach(system.diagnostics.process Pro在PROC) { kill()亲; } DirectoryInfo文件夹=新的DirectoryInfo(路径。gettemppath() +:jurassicupdate”); 如果(文件夹。存在) { foreach(FileInfo文件在文件夹。getfiles()) { / /如果临时文件夹下存在与应用程序所在目录下的文件同名的文件,则删除应用程序目录下的文件 如果(文件是否存在(application.startuppath +“+路径。作用: 取文件名(文件。这))) 文件。删除(application.startuppath +“\”+路径。作用:取文件名(文件。全名)); / /将临时文件夹的文件移到应用程序所在的目录下 文件(文件。移动。这、application.startuppath +“+路径。 GetFileName (theFile.FullName)); } } / / start Setup This.labDownFile.Text = "starting the program..."; System.Diagnostics.Process.Start (Application.StartupPath + "\ \" + "TIMS.exe"); (this.Close); } }
本文档为【c 实现软件自动更新思路(The idea of automatic updating software by c )】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_751406
暂无简介~
格式:doc
大小:39KB
软件:Word
页数:14
分类:生活休闲
上传时间:2018-02-15
浏览量:37