C#升级WinForm系统的完全代码.docx
- 文档编号:30592457
- 上传时间:2023-08-18
- 格式:DOCX
- 页数:15
- 大小:80.47KB
C#升级WinForm系统的完全代码.docx
《C#升级WinForm系统的完全代码.docx》由会员分享,可在线阅读,更多相关《C#升级WinForm系统的完全代码.docx(15页珍藏版)》请在冰豆网上搜索。
C#升级WinForm系统的完全代码
C#winform升级的详细代码
以我以前做过的人事系统(HrManager.exe)为例:
★客户端安装目录下,添加一个xml文件,命名为:
update.xml。
update.xml的内容如下:
xmlversion="1.0"encoding="gb2312"?
>
★以下是更新升级程序的说明以及源代码:
在VisualStudio2010中(VS2005以上版本也可以),新建winForms应用程序,命名为Update,
删除自动生成的Form1窗体。
新建窗体FrmUpdate。
在FrmUpdate中放入三个控件:
Button:
btnUpdate(更新按钮)。
ProgressBar:
progressBar1(进度条)。
RichTextBox:
rchtxtInfo。
如下图所示:
在Update的应用程序的bin\Debug下,添加一个xml文件,命名为update.xml。
bin\Debug\update.xml的内容如下:
xmlversion="1.0"encoding="gb2312"?
>
其中Url代表服务器地址,AppName:
应用程序名,AppVersion:
应用程序版本
File需要更新的文件
窗体FrmUpdate的代码如下(窗体自动生成的代码不显示,只用绑定btnUpdate按钮的Click事件btnUpdate_Click即可)。
★源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Xml;
usingSystem.IO;
usingSystem.Net;
usingSystem.Diagnostics;
namespaceUpdate
{
publicpartialclassFrmUpdate:
Form
{
publicFrmUpdate()
{
InitializeComponent();
progressBar1.Visible=false;//进?
度¨¨条¬?
控?
件t隐°t藏?
}
///
///按ã¡ä钮£¤"更¨¹新?
"的Ì?
Click事º?
件t
///
///
///
privatevoidbtnUpdate_Click(objectsender,EventArgse)
{
stringappName=GetAppName();//应®|用®?
程¨¬序¨°进?
程¨¬名?
KillAppProcess(appName);//如¨?
果?
应®|用®?
程¨¬序¨°正y在¨²运?
行D,ê?
则¨°强?
行D关?
闭À?
。
¡ê
FolderBrowserDialogfbd=newFolderBrowserDialog();
fbd.Description="请?
选?
择?
程¨¬序¨°所¨´在¨²的Ì?
目?
录?
";
fbd.SelectedPath=@"E:
\Hr";
if(fbd.ShowDialog()==DialogResult.OK)
{
//查¨¦看¡ä选?
择?
的Ì?
目?
录?
是º?
否¤?
含?
有®DHrManager.exe
if(!
File.Exists(fbd.SelectedPath+"\\"+appName))
{
MessageBox.Show("目录错误.请选择应用程序所在的目录","提示º?
");
return;
}
stringurl=GetUrl();
if(!
Directory.Exists(url))//查¨¦看¡ä本À?
机¨²客¨ª户¡ì端?
能¨¹否¤?
连¢?
接¨®服¤t务?
器¡Â目?
录?
{
MessageBox.Show("无法连接到服务器...","提示");
return;
}
//服¤t务?
器¡Â的Ì?
update.xml与®?
客¨ª户¡ì端?
的Ì?
update.xml比À¨¨较?
版ã?
本À?
号?
是º?
否¤?
一°?
致?
stringserverVersion=GetServerVersion();
stringclientVersion=GetClientVersion(fbd.SelectedPath);
rchtxtInfo.Text="服务器版本:
"+serverVersion+"\n客户端版本:
"+clientVersion+"\n";
Application.DoEvents();
if(serverVersion==clientVersion)
{
rchtxtInfo.Text=rchtxtInfo.Text+"已经是最新版本,不需要更新.\n";
Application.DoEvents();
return;
}
//本À?
地Ì?
版ã?
本À?
与®?
服¤t务?
器¡Â版ã?
本À?
不?
一°?
致?
需¨¨要°a更¨¹新?
this.Cursor=Cursors.WaitCursor;
progressBar1.Visible=true;//滚?
动¡¥条¬?
显?
示º?
List
for(inti=0;i { if(DownUpdateFile(url+list[i],fbd.SelectedPath+"\\"+list[i])==false) { rchtxtInfo.Text=rchtxtInfo.Text+"更新中出现错误."; this.Cursor=Cursors.Default; return; } } rchtxtInfo.Text=rchtxtInfo.Text+"全部更新已完成."; //更¨¹新? 本À? 地Ì? 客¨ª户¡ì端? 下? 的Ì? update.xml的Ì? 版ã? 本À? 号? ,ê? 与®? 服¤t务? 端? 版ã? 本À? 号? 一°? 致? UpdateLocalXml(fbd.SelectedPath+"\\update.xml",serverVersion); progressBar1.Visible=false;//滚? 动¡¥条¬? 隐°t藏? this.Cursor=Cursors.Default; } } /// ///更¨¹新? 本À? 地Ì? 的Ì? 配? 置? 文? 件t的Ì? 版ã? 本À? 号? 使º1版ã? 本À? 号? 与®? 服¤t务? 器¡Â版ã? 本À? 一°? 致? 这a样¨´下? 次ä? 就¨ª不? 必À? 更¨¹新? 了¢? /// /// 地Ì? xml的Ì? 完ª¨º全¨? 路¡¤径? /// 器¡Â版ã? 本À? 号? privatevoidUpdateLocalXml(stringxmlFile,stringversion) { XmlDocumentdoc=newXmlDocument(); doc.Load(xmlFile); XmlNoderootNode=doc.DocumentElement; for(inti=0;i { if(rootNode.ChildNodes[i].Name=="Application") { rootNode.ChildNodes[i].ChildNodes[1].InnerText=version; break; } } doc.Save(xmlFile); } /// ///如¨? 果? 应®|用®? 程¨¬序¨°正y在¨²运? 行D,ê? 则¨°强? 行D关? 闭À? 。 ¡ê然¨? 后¨®才? 能¨¹更¨¹新? /// /// privatevoidKillAppProcess(stringappName) { Process[]allProcess=Process.GetProcesses(); foreach(ProcesspinallProcess) { //MessageBox.Show(p.ProcessName);//进? 程¨¬名? 称? 没? 有®D".exe" if(p.ProcessName.ToLower()+".exe"==appName.ToLower()) { for(inti=0;i { p.Threads[i].Dispose(); } p.Kill(); } } } /// ///获? 得Ì? 服¤t务? 器¡Â下? 载? 地Ì? 址¡¤ /// /// privatestringGetUrl() { XmlDocumentdoc=newXmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory+"update.xml"); XmlNoderootNode=doc.DocumentElement; returnrootNode.ChildNodes[0].InnerText; } /// ///通ª¡§过yxml文? 件t获? 得Ì? 应®|用®? 程¨¬序¨°名? /// /// privatestringGetAppName() { XmlDocumentdoc=newXmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory+"update.xml"); XmlNoderootNode=doc.DocumentElement; for(inti=0;i { if(rootNode.ChildNodes[i].Name=="Application") { returnrootNode.ChildNodes[i].ChildNodes[0].InnerText; } } return""; } /// ///获? 得Ì? 服¤t务? 器¡Â版ã? 本À? 号? /// /// privatestringGetServerVersion() { XmlDocumentdoc=newXmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory+"update.xml"); XmlNoderootNode=doc.DocumentElement; for(inti=0;i { if(rootNode.ChildNodes[i].Name=="Application") { returnrootNode.ChildNodes[i].ChildNodes[1].InnerText; } } return""; } /// ///获? 得Ì? 客¨ª户¡ì端? 版ã? 本À? 号? /// /// 所¨´安ã2装Á¡ã的Ì? 目? 录? /// privatestringGetClientVersion(stringdirectoryPath) { XmlDocumentdoc=newXmlDocument(); doc.Load(directoryPath+"\\update.xml"); XmlNoderootNode=doc.DocumentElement; for(inti=0;i { if(rootNode.ChildNodes[i].Name=="Application") { returnrootNode.ChildNodes[i].ChildNodes[1].InnerText; } } return""; } /// ///从䨮服¤t务? 器¡Â中D获? 取¨? 更¨¹新? 文? 件t列¢D表À¨ª /// /// privateList { List XmlDocumentdoc=newXmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory+"update.xml"); XmlNoderootNode=doc.DocumentElement; for(inti=0;i { if(rootNode.ChildNodes[i].Name=="Files")//找¨°到Ì? Files节¨²点Ì? { for(intj=0;j { //找¨°出? 节¨²点Ì? 的Ì? Name属º? 性? XmlAttributeattr=rootNode.ChildNodes[i].ChildNodes[j].Attributes["Name"]; list.Add(attr.Value); } break; } } returnlist; } /// ///下? 载? 并¡é更¨¹新? 文? 件t(本À? 地Ì? 客¨ª户¡ì端? 存ä? 在¨²文? 件t就¨ª用®? 服¤t务? 器¡Â文? 件t覆2盖? 掉Ì? 不? 存ä? 在¨²该? 文? 件t就¨ª创ä¡ä建¡§并¡é写¡ä入¨? ) /// /// 器¡Â文? 件t全¨? 名? /// 地Ì? 文? 件t全¨? 名? /// privateboolDownUpdateFile(stringserverFullName,stringlocalFullName) { try { WebClientwcClient=newWebClient(); //服¤t务? 器¡Â文? 件t全¨? 名? : url+list[i] WebRequestwebReq=WebRequest.Create(serverFullName); WebResponsewebRes=webReq.GetResponse(); longfileLength=webRes.ContentLength; stringfileName=Path.GetFileName(serverFullName); rchtxtInfo.Text=rchtxtInfo.Text+string.Format("正在下载更新文件: {0},文件大小: {1}KB\n",fileName,(fileLength/1024.0).ToString("N2")); progressBar1.Maximum=(int)fileLength; progressBar1.Value=0; Application.DoEvents(); Streamstream=webRes.GetResponseStream(); StreamReadersr=newStreamReader(stream); byte[]bufferbyte=newbyte[fileLength]; intallByte=(int)bufferbyte.Length; intstartByte=0; while(fileLength>0) { intdownByte=stream.Read(bufferbyte,startByte,allByte); if(downByte==0) { break; } startByte=startByte+downByte; allByte=allByte-downByte; progressBar1.Value=startByte; //即¡ä: progressBar1.Value=progressBar1.Value+downByte; } //本À? 地Ì? 文? 件t存ä? 在¨²则¨°覆2盖? 不? 存ä? 在¨²则¨°创ä¡ä建¡§ //stringlocalFile=fbd.SelectedPath+"\\"+list[i]; CreateNewDiretory(localFullName); //创ä¡ä建¡§或¨°更¨¹新? 覆2盖? 文? 件t FileStreamfs=newFileStream(localFullName,FileMode.Create,FileAccess.Write); fs.Write(bufferbyte,0,bufferbyte.Length); stream.Close(); sr.Close(); fs.Close(); rchtxtInfo.Text=rchtxtInfo.Text+string.Format("更新文件成功: {0}\n",fileName); Application.DoEvents(); returntrue; } catch(Exceptionex) { MessageBox.Show(ex.Message,"更新中出现错误"); returnfalse; } } /// ///如¨? 果? 目? 录? 和¨ª文? 件t在¨²新? 的Ì? 目? 录? 中D,则¨°创ä¡ä建¡§目? 录? /// /// privatevoidCreateNewDiretory(stringpath) { if(! File.Exists(path)) { string[]dirArray=path.Split('\\'); stringtemp=string.Empty; for(inti=0;i { temp+=dirArray[i].Trim()+"\\"; if(! Directory.Exists(temp))//如果目录不存在,就依次创建 { Directory.CreateDirectory(temp); } } } } } } ★运行结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 升级 WinForm 系统 完全 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)