
下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
自动更新的软件的目的在于让客户不在为了寻找最新软件花费时间。也不用去到开发商的网站上查找。客户端的软件自动会在程序启动前查找服务器上最新的版本。 和自己当前软件的版本比较,如果服务器的是最新版本。客户端则进行自动下载、解压、安装。当然了下载是要有网络的,并且用户可以根据提示去完成 *** 作。再也 不用为找不到最新版本的软件而头疼。下面是我的大体思路,已经得到了实现:1、 写一个webservice,提供一个获取服务器xml中版本的数据的方法。|
<?xml version="1.0" enCoding="utf-8" ?><Content> <Project ID="程序名称" Edition="1.0"> </Project></Content>2、 在WinForm应用程序启动的时候,首先访问webservice获取服务器的xml中的版本号,然后获取客户端的xml中的版本号。将两个版本号比较,若服务器中的版本号大,则提示提示可以在线更新应用程序。
3、 然后客户端自动下载网络上的zip压缩包到本机的指定位置,进行自动解压缩到安装的目录进行覆盖。解压缩完毕之后,用进程打开所解压过的exe文件进行软件安装。同时关闭客户端软件所在的进程。
4、注意:升级程序和应用程序都是单独的,应用程序在使用时不能对本身进行升级(覆盖会失败)。
具体代码如下:
第一部分 应用程序如口Program:
using System;using System.Collections.Generic;using System.windows.Forms;using Medicine_ERP;using DevExpress.XtraEditors;using System.Xml;using AnshIEld_autoFutures.BaseClase;namespace AnshIEld_autoFutures{ static class Program { private static voID LoadMath() { //服务器上的版本号 string NewEdition = "1.0"; //应用程序中的版本号 string oldEdition = "1.0"; try { //服务器上的版本号 NewEdition = webserverClase.getcopyRightCode(); //获取系统中xml里面存储的版本号 String filename = Application.StartupPath + "\XMLEdition.xml"; Xmldocument xmlDoc = new Xmldocument(); xmlDoc.Load(filename); XmlNode xn = xmlDoc.SelectSingleNode("Content"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { XmlElement xe = (XmlElement)xnf; if (xe.GetAttribute("ID") == "jigou_plz") { oldEdition = xe.GetAttribute("Edition");//动态数组 } break; } double newE = double.Parse(NewEdition); double oldE = double.Parse(oldEdition); //比较两个版本号,判断应用程序是否要更新 if (newE > oldE) { //更新程序¨ DialogResult dr = XtraMessageBox.Show("发现新的版本是否要更新该软件","平浪舟现货程序化交易--更新提示",MessageBoxbuttons.OKCancel,MessageBoxIcon.information); if (dr == DialogResult.OK) { //打开下载窗口 // Application.Run(new DownUpdate()); //启动安装程序 System.Diagnostics.Process.Start(Application.StartupPath + @"\Upgrade_Form.exe"); Application.Exit(); } else { //若用户取消,打开初始界面 anshIEld_Login login = new anshIEld_Login(); if (login.ShowDialog() == DialogResult.OK) { Application.Run(new Main_autoFutures()); } } } else { //若服务器版本低于或相等客户端,打开初始界面 anshIEld_Login login = new anshIEld_Login(); if (login.ShowDialog() == DialogResult.OK) { Application.Run(new Main_autoFutures()); } } } catch { XtraMessageBox.Show("网络链接失败!","提示",MessageBoxbuttons.OK,MessageBoxIcon.Error); } } /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static voID Main() { //保证同时只有一个客户端在运行 System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex(false,"jigou_plz.exe"); if (!mutexMyapplication.WaitOne(100,false)) { XtraMessageBox.Show("程序" + Application.Productname + "已经在运行!","警告",MessageBoxIcon.Warning); return; } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //汉化 hugengyong hu = new hugengyong(); DevExpress.UserSkins.OfficeSkins.Register(); DevExpress.Skins.SkinManager.EnableFormSkins(); DevExpress.UserSkins.BonusSkins.Register(); LoadMath(); } } }} 第二部分:升级程序代码如下: //deBUG目录,用于存放压缩文件 string path = Application.StartupPath; private voID btnDown_Click(object sender,EventArgs e) { string zipfile = path + @"\jigou_plz.zip"; //关闭原有的应用程序 killProess(); btnDown.Enabled = false; button2.Enabled = false; //自动下载压缩包,并且解压,最后关闭当前进程,进行安装 try { //下载自动更新 string downUrl = ConfigurationManager.ConnectionStrings["DownUrl"].ConnectionString.ToString().Trim(); if (!string.IsNullOrEmpty(downUrl)) { Downloadfile(downUrl,zipfile,progressbar1,label1); } else { MessageBox.Show("Config中的下载路径配置错误!","错误",MessageBoxIcon.Error); return; } } catch { MessageBox.Show("当前没有网络或者文件地址不正确"); return; } //解a压1 try { //关闭原有的应用程序 killProess(); //unCompressRAR(path,path,"setup.rar",true); BaseClase.Zip.UnZip(zipfile,"",true,true); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } if (MessageBox.Show("升级完成!,请重新登陆!",MessageBoxIcon.information) == DialogResult.OK) { fileInfo file = new fileInfo(path + @"\AnshIEld_autoFutures.exe");//文件地址 if (file.Exists) { Process.Start(path + @"\AnshIEld_autoFutures.exe"); } Application.Exit(); } } /// <summary> /// c#.net 下载文件 /// </summary> /// <param name="URL">下载文件地址</param> /// /// <param name="filename">下载后的存放地址</param> /// <param name="Prog">用于显示的进度条</param> /// public voID Downloadfile(string URL,string filename,System.windows.Forms.Progressbar prog,System.windows.Forms.Label label1) { float percent = 0; try { System.Net.httpWebRequest Myrq = (System.Net.httpWebRequest)System.Net.httpWebRequest.Create(URL); System.Net.httpWebResponse myrp = (System.Net.httpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.fileStream(filename,System.IO.fileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by,(int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.windows.Forms.Application.DoEvents(); so.Write(by,osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by,(int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; label1.Text = "下载进度" + percent.ToString() + "%"; System.windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } label1.Text = "下载完成。安装中... ..."; so.Flush();//将缓冲区内在写入到基础流中 st.Flush();//将缓冲区内在写入到基础流中 so.Close(); st.Close(); } catch (System.Exception) { throw; } } /// <summary> /// 关闭原有的应用程序 /// </summary> private voID killProess() { this.label1.Text = "正在关闭程序...."; System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByname("AnshIEld_autoFutures"); //关闭原有应用程序的所有进程 foreach (System.Diagnostics.Process pro in proc) { pro.Kill(); } } zip压缩文件解压方法类 using System;using System.Collections.Generic;using System.Text;using System.IO;using System.IO.Compression;using ICSharpCode.SharpZiplib.Zip;namespace Upgrade_Form.BaseClase{ class Zip { /// <summary> /// 解压缩一个 zip 文件。 /// </summary> /// <param name="zipedfile">zip文件路径</param> /// <param name="strDirectory">解压路径</param> /// <param name="password">zip文件的密码</param> /// <param name="overWrite">是否覆盖已存在的文件。</param> /// <param name="deltefile">解压后是否删除文件</param> public static voID UnZip(string zipedfile,string strDirectory,string password,bool overWrite,bool deltefile) { //路径不存在则创建 if (Directory.Exists(strDirectory) == false) { Directory.CreateDirectory(strDirectory); } if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\")) strDirectory = strDirectory + "\"; using (ZipinputStream s = new ZipinputStream(file.OpenRead(zipedfile))) { s.Password = password; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryname = ""; string pathToZip = ""; pathToZip = theEntry.name; if (pathToZip != "") directoryname = Path.GetDirectoryname(pathToZip) + "\"; string filename = Path.Getfilename(pathToZip); Directory.CreateDirectory(strDirectory + directoryname); if (filename != "") { if ((file.Exists(strDirectory + directoryname + filename) && overWrite) || (!file.Exists(strDirectory + directoryname + filename))) { using (fileStream streamWriter = file.Create(strDirectory + directoryname + filename)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data,data.Length); if (size > 0) streamWriter.Write(data,size); else break; } streamWriter.Close(); } } } } s.Close(); } if (deltefile == true) { file.Delete(zipedfile); } } }} 以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的C# 程序自动升级的代码全部内容,希望文章能够帮你解决C# 程序自动升级的代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)