实例介绍文件下载,注册表操作等基本技巧!(原创)

来源:互联网 发布:fifaol3数据库中文版 编辑:程序博客网 时间:2024/05/07 05:30

??? 本人学.NET时间也不是很长,写一点东东出来,大家共享,错误之处肯请指出。好了进入正题。

??? 这个小程序是我为一个朋友的公司做的,本身技术含量不是很高,都是一些C#简单的应用,在制作

的过程中特别体会到用.NET开发Windows应用程序的方便和快捷。

??? 程序的功能就是完成对应用程序的升级。思路就是把需要升级的文件打成压缩包,升级程序通过把

??? 压缩包下载到本地后再自动解包,把文件覆盖到应用程序的指定目录。这个过程中还提到现两个问

题就是在更新的时候要把应用程序的主程序给关闭,升级完了还要再启动。下面就请看整个的实现:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;
using System.Diagnostics;
using Microsoft.Win32;
using System.Runtime.InteropServices;

[DllImport("unZip.dll")]
??public static extern int MyZip_ExtractFileAll(String ZipFile,String PathName);//这个声明要放在窗体类里

private void Form1_Load(object sender, System.EventArgs e)
??{
???Thread ds_test = new Thread(new ThreadStart(test));//建立了一个线程
???ds_test.IsBackground=true;
???ds_test.Start();??

????/* 整个程序也就是从这里开始。在Form1_Load事件里我抛出了一个线程来执行测试网络连通性,因为在Form1_Load事件里写太多的代码,我怕影响窗体启动的速度。*/
??}
??//测试网络是否连通的函数
??private void test()
??{???
???label1.ForeColor=System.Drawing.Color.Red;//标签显示“正在检测网络配置”
???try??? //用try和catch结构来获得ServerAddress.GetResponse()的异常,给出相应的提示
???{
????WebRequest ServerAddress=WebRequest.Create("http://www.zz.ha.cn/");
????ServerAddress.GetResponse();//返回对Internet请求的响应
????label6.Text="服务器已连接";??

?? }

catch
???{
????MessageBox.Show("网络连接失败,请确定您的机器已经接入互联网","鼎晟智能升级程序",MessageBoxButtons.OK,MessageBoxIcon.Warning);
????Application.Exit();???
???}
????label2.ForeColor=System.Drawing.Color.Red;//标签显示“正在关闭当前打开的应用程序”

????KillProcess();??//调用关闭进程的函数,来关闭应用程序

????Thread ds_downlistfile = new Thread(new ThreadStart(downlistfile));//抛出新的线程来下载升级包
????ds_downlistfile.IsBackground=true;
????ds_downlistfile.Start();
???}?
??}

//下载升级文件包
??private void downlistfile()
??{
??????? label3.ForeColor=System.Drawing.Color.Red; //下载升级文件
???????
???????string pathName=ReadReg();//获得安装程序的位置
?????? string zipFile=pathName+"//ltxupgrade.zip";?//指定下载到本地的路径????
?????? string myStringWebResource=null;
?????? myStringWebResource="http://www.ds-software.net/soft/ganzhou/ltxupgrade.zip";?//指定网上文件的地址
?????? WebClient myWebClient=new WebClient();
????try
????{
????????myWebClient.DownloadFile(myStringWebResource,zipFile);??//从指定的地址下载文件到本地
?????}
????catch
????{
?????MessageBox.Show("下载升级包没有成功!请重新启动本升级程序!","鼎晟智能升级程序",MessageBoxButtons.OK,MessageBoxIcon.Warning);
?????Application.Exit();?????
????}
???label4.ForeColor=System.Drawing.Color.Red;
???unZipFile(zipFile,pathName);//因为升级文件是在压缩包里所调用解压函数把文件解出来
??}
??

//把升级包中的文件覆盖到程序目录

//这里使用了unZip.dll,这个dll可以完成压缩和解压缩的功能。采用标准的压缩模式。
??private void unZipFile(string zipFile,string pathName)
??{
???int m=MyZip_ExtractFileAll(zipFile,pathName);
???if(m<0)
???{
????MessageBox.Show("更新文件失败!","鼎晟软件智能升级程序");
????Application.Exit();
???}
???else if(m==0)
???{
????MessageBox.Show("升级包中存在错误,请联系鼎晟科技技术服务!服务电话0371-7438877","鼎晟软件智能升级程序",MessageBoxButtons.OK,MessageBoxIcon.Warning);
????Application.Exit();
???}
???MessageBox.Show("恭喜您!软件已经更新成为最新版本,谢谢您的使用!确认后升级程序将启动先前关闭的程序","鼎晟软件智能升级程序");
???startgz_app();?//调用启动应用程序的函数
???Application.Exit();???
??}
??//杀掉工资软件的进程
??private void KillProcess()
??{
???System.Diagnostics.Process myproc= new System.Diagnostics.Process();
???//得到所有打开的进程
???try
???{
????foreach (Process thisproc in Process.GetProcessesByName("ltx"))?

//应用程序在进程列表里的名字 ltx ,遍历整个进程列表,找到这个名字的进程,
????{?????
??????thisproc.Kill();??//调用kill()方法杀掉进程???????????
????}
???}
???catch
???{
????MessageBox.Show("试图关闭当前鼎晟工资软件失败!请先关闭,然后重新启动本升级程序!","鼎晟智能升级程序");
????Application.Exit();???
???}
??}
//启动应用程序的函数???

private void startgz_app()
??{
???System.Diagnostics.ProcessStartInfo? Info? =? new? System.Diagnostics.ProcessStartInfo();???
???Info.FileName? =? "ltx.exe";

?? String KeyValue=ReadReg();
???Info.WorkingDirectory? = KeyValue;
???System.Diagnostics.Process? Proc? ;
???try
???{
????Proc? =? System.Diagnostics.Process.Start(Info);
????oprogress(100);
????
???}
???catch
???{
????MessageBox.Show("找不到执行文件,请安装鼎晟人事工资管理系统","鼎晟智能升级");
???}
??}

//读取注册表的apppath获得应用程序的安装路径

private?string?ReadReg()

{

? ?RegistryKey reg1=Registry.LocalMachine;
???RegistryKey reg2=reg1.OpenSubKey("SOFTWARE");
???RegistryKey reg3=reg2.OpenSubKey("离退休人员管理系统");//应用程序在注册表里的名称
???String AppPath=reg3.GetValue("apppath").ToString();??//获得apppath的值

?? return AppPath;//返回应用程序所在的路径

}
//在关闭事件里提供判断防止用户无意关闭升级程序

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
??{
???
???if (MessageBox.Show("您是否要退出升级程序","鼎晟智能升级程序",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
????== DialogResult.Yes)
???{
????Application.Exit();
???}
???else
???{
????e.Cancel=true;
???}???
??}???
?}

到这里整个程序就完成了,希望这个简单的应用程序能给需要的人带来帮助。

关于应用程序调用的unZip.dll我将专门写出它的声明方法,并提供文件的下载。