知识大全 c#在线升级

Posted 文件

篇首语:三人行必有我师焉;择其善者而从之,其不善者而改之。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 c#在线升级相关的知识,希望对你有一定的参考价值。

  面介绍一种用Web Services制作的升级程序 通过Web Services 升级程序就象读写本机文件一样简单 所以我就直接给出代码

  Web Services部分代码     using System;    using System Web;    using System Web Services;    using System Web Services Protocols;    using System IO;

  [WebService(Namespace = )]    [WebServiceBinding(ConformsTo = WsiProfiles BasicProfile _ )]    public class Service : System Web Services WebService            public Service()                    //如果使用设计的组件 请取消注释以下行            //InitializeComponent();                /// <summary>        /// 需要升级文件的服务器路径        /// </summary>        private const string UpdateServerPath = d:\\\\Debug ;        [WebMethod(Description = 返回服务器上程序的版本号 )]        public string ServerVer()                    return ;                [WebMethod(Description = 返回需更新的文件 )]        public string[] NewFiles()                    DirectoryInfo di = new DirectoryInfo(UpdateServerPath);            FileInfo[] fi = di GetFiles();            int intFiles= fi Length;            string[] myNewFiles = new string[intFiles];            int i = ;            foreach (FileInfo fiTemp in fi)                            myNewFiles[i] = fiTemp Name;                System Diagnostics Debug WriteLine(fiTemp Name);                i++;           

  return myNewFiles;                [WebMethod(Description = 返回需更新的文件的大小 )]        public int AllFileSize()                    int filesize = ;            string[] files = Directory GetFiles(UpdateServerPath);            foreach (string file in files)                            FileInfo myInfo = new FileInfo(file);                filesize += (int)myInfo Length / ;                        return filesize;       

  [WebMethod(Description = 返回给定文件的字节数组 )]        public byte[] GetNewFile(string requestFileName)                    ///得到服务器端的一个文件            if (requestFileName != null || requestFileName != )                return getBinaryFile(UpdateServerPath + \\\\ +requestFileName);            else                return null;       

  /// <summary>        /// 返回所给文件路径的字节数组         /// </summary>        /// <param name= filename ></param>        /// <returns></returns>        private byte[] getBinaryFile(string filename)                    if (File Exists(filename))                            try                                    //打开现有文件以进行读取                     FileStream s = File OpenRead(filename);                    return ConvertStreamToByteBuffer(s);                                catch                                    return new byte[ ];                                        else                            return new byte[ ];                            /// <summary>        /// 把给定的文件流转换为二进制字节数组         /// </summary>        /// <param name= theStream ></param>        /// <returns></returns>        private byte[] ConvertStreamToByteBuffer(System IO Stream theStream)                    int b ;            System IO MemoryStream tempStream = new System IO MemoryStream();            while ((b = theStream ReadByte()) != )                            tempStream WriteByte(((byte)b ));                        return tempStream ToArray();       

  

  升级程序代码     using System;    using System Collections Generic;    using System ComponentModel;    using System Data;    using System Drawing;    using System Text;    using System Windows Forms;    using System Threading;    using System Xml;    using System IO;    using System Diagnostics;

  namespace AutoUpdate            public partial class frmAutoUpdate : Form                    public frmAutoUpdate()                            InitializeComponent();                        /// <summary>            /// 当前版本            /// </summary>            private string m_strLocalVer;            /// <summary>            /// 服务器版本            /// </summary>            private string m_strServerVer;            /// <summary>            /// 需要更新的文件总数            /// </summary>            private int m_intFilesCount = ;            /// <summary>            /// 需要更新的文件大小            /// </summary>            private int m_AllFileSize;            /// <summary>            /// 正在下载的文件大小            /// </summary>            private int m_downingFileSize;            /// <summary>            /// 已经下载的文件大小            /// </summary>            private int m_downedFileSize;            /// <summary>            ///数组 需要更新的文件            /// </summary>            private string[] m_Files = null;            /// <summary>            /// 定义文件对象            /// </summary>            WebReference Service ws;            /// <summary>            /// 连接WebServeces并下载文件线程            /// </summary>            private Thread DownThread;            private void frmAutoUpdata_Load(object sender EventArgs e)                            DownThread = new Thread(new ThreadStart(StartUpdate));                DownThread Start();                        /// <summary>            /// 连接服务器 开始升级            /// </summary>            private void StartUpdate()                            ws = new WebReference Service();                m_AllFileSize = ws AllFileSize();                try                                    m_strLocalVer =Config Ver; //从配置文件中读取当前版本号                    m_strServerVer = ws ServerVer();//保存服务器版本号

  BeginInvoke(new System EventHandler(UpdateUI) lblLink);                    //当前版本低于服务器版本 开始更新                     if (double Parse(m_strLocalVer) < double Parse(m_strServerVer))                                            BeginInvoke(new System EventHandler(UpdateUI) lblDownLoad);                        m_Files = ws NewFiles();//需要更新的文件                        if (m_Files != null)                                                    m_intFilesCount = m_Files Length;                            for (int i = ; i <= m_intFilesCount ; i++)                                                            DownFile(m_Files[i]);                                Debug WriteLine(m_Files[i]);                                                        // Config VER = strServerVer;//将版本号记录到配置文件中                                                                else                                            BeginInvoke(new EventHandler(OnShowMessage)                             你计算机上安装的已经是最新版本了 不需要升级 );                        return;                                                    catch (Exception ex)                                    BeginInvoke(new EventHandler(OnShowMessage) ex Message);                    return;                                //UI线程设置label属性                BeginInvoke(new System EventHandler(UpdateUI) lblStartExe);                //安装文件                MethodInvoker miSetup = new MethodInvoker(this StartSetup);                this BeginInvoke(miSetup);                        /// <summary>            /// 下载文件            /// </summary>            /// <param name= fileName >文件名</param>            private void DownFile(string fileName)                            //得到二进制文件字节数组                 byte[] bt = ws GetNewFile(fileName);                m_downingFileSize = bt Length / ;                string strPath = Application StartupPath + \\\\Update\\\\ + fileName;                if (File Exists(strPath))                                    File Delete(strPath);                                FileStream fs = new FileStream(strPath FileMode CreateNew);                fs Write(bt bt Length);                fs Close();                        /// <summary>            /// 开始安装下载的文件            /// </summary>           private void StartSetup()                            // 结束当前运行的主程序                Process[] pros = Process GetProcesses();                for (int i = ; i < pros Length; i++)                                    if (pros[i] ProcessName == 教师考勤系统 )                                            pros[i] Kill();                                                    // 开始复制文件                for (int i = ; i <= m_intFilesCount ; i++)                                    string sourceFileName = Application StartupPath + \\\\Update\\\\ + m_Files[i];                    string destFileName = Application StartupPath + \\\\ + m_Files[i];                    if (File Exists(destFileName))                                            File Delete(destFileName);                                        File Copy(sourceFileName destFileName false);                                // 升级完成 启动主程序                string StrExe = Application StartupPath + \\\\教师考勤系统 exe ;

  if (File Exists(StrExe))                                    System Diagnostics Process Start(StrExe);               

  //关闭升级程序                Application Exit();           

  #region 辅助方法 确保线程安全

  /// <summary>            /// Label上显示信息            /// </summary>            private void UpdateUI(object sender EventArgs e)                            Label lbl = (Label)sender;                lbl ForeColor = SystemColors Desktop;                lblLocalVer Text = m_strLocalVer;                lblServerVer Text = m_strServerVer;           

  /// <summary>            /// 显示对话框            /// </summary>            private void OnShowMessage(object sender EventArgs e)                            MessageBox Show(this sender ToString() this Text                     MessageBoxButtons OK MessageBoxIcon Information);                Thread Sleep( );                this Close();                        /// <summary>            /// 显示进度条            /// </summary>            private void InvokePrgBar()                            if (prgBar Value < )                                    prgBar Value = * (int)m_downedFileSize / m_AllFileSize;                    prgBar Update();                    System Diagnostics Debug WriteLine( prgBar Value: + prgBar Value);                                        /// <summary>            /// 计算文件已下载的百分比            /// </summary>            private void subPrgBar()                            m_downedFileSize += m_downingFileSize;                System Diagnostics Debug WriteLine( AllFileSize  + m_AllFileSize);                System Diagnostics Debug WriteLine( downingFileSize  + m_downingFileSize);                do                                    Thread Sleep( );                    MethodInvoker mi = new MethodInvoker(this InvokePrgBar);                    this BeginInvoke(mi);                while (m_AllFileSize <= m_downedFileSize);                        #endregion

cha138/Article/program/net/201311/11273

相关参考

知识大全 C#语言学习:C#数据报编程之测试程序

C#语言学习:C#数据报编程之测试程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  C#中自定

知识大全 叩开C#之门系列之C#与面向对象编程语言

叩开C#之门系列之C#与面向对象编程语言  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  C#是纯

知识大全 C#类型

展现C#世界之四:C#类型  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  第四章C#类型  既然

知识大全 让自己学会写的一个在线升级的程序

  voidCUpdateDlg::OnButtonDoupdate()      //读取升级文件  CFileStatusrStatus;  CStringszIniData;  CStringi

知识大全 C#特性学习与使用

C#特性学习与使用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  C#特性以前的时候用过C#中的

知识大全 C#反射浅析

C#反射浅析  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!首先了解C#反射的概念反射是一个运行库类

知识大全 C#基本语法

C#基本语法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  俗话说无规矩不成方圆C#是一种编程语

知识大全 C#迭代器

C#迭代器  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  迭代器是C#中的新功能迭代器是方法ge

知识大全 C#面试题

C#面试题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!一填空题c#中的三元运算符是_____?当

知识大全 C#基本语法简介

C#基本语法简介  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一系统数据类型和C#简化符号