转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> C语言系列 >> 正文
C#用Web Services制作的升级程序         ★★★

C#用Web Services制作的升级程序

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1113 更新时间:2012/2/27 16:43:17
C#用Web Services制作的升级程序

以下是代码片段:
   Web Services部分代码:
    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.IO;

    [WebService(Namespace = "http://www.mintao.net//")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        public Service()
        {
            //如果使用设计的组件,请取消注释以下行
            //InitializeComponent();
        }
        /// <summary>
        /// 需要升级文件的服务器路径
        /// </summary>
        private const string UpdateServerPath ="d:\\Debug";
        [WebMethod(Description = "返回服务器上程序的版本号")]
        public string ServerVer()
        {
            return "4.0";
        }
        [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 = 0;
            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 = 0;
            string[] files = Directory.GetFiles(UpdateServerPath);
            foreach (string file in files)
            {
                FileInfo myInfo = new FileInfo(file);
                filesize += (int)myInfo.Length / 1024;
            }
            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[0];
                }
            }
            else
            {
                return new byte[0];
            }
        }
        /// <summary>
        /// 把给定的文件流转换为二进制字节数组。
        /// </summary>
        /// <param name="theStream"></param>
        /// <returns></returns>
        private byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
        {
            int b1;
            System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
            while ((b1 = theStream.ReadByte()) != -1)
            {
                tempStream.WriteByte(((byte)b1));
            }
            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 = 0;
            /// <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 = 0; i <= m_intFilesCount - 1; 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 / 1024;
                string strPath = Application.StartupPath + "\\Update\\" + fileName;
                if (File.Exists(strPath))
                {
                    File.Delete(strPath);
                }
                FileStream fs = new FileStream(strPath, FileMode.CreateNew);
                fs.Write(bt, 0, bt.Length);
                fs.Close();
            }
            /// <summary>
            /// 开始安装下载的文件
            /// </summary>
           private void StartSetup()
            {
                // 结束当前运行的主程序
                Process[] pros = Process.GetProcesses();
                for (int i = 0; i < pros.Length; i++)
                {
                    if (pros[i].ProcessName == "教师考勤系统")
                    {
                        pros[i].Kill();
                    }
                }
                // 开始复制文件
                for (int i = 0; i <= m_intFilesCount - 1; 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(100);
                this.Close();
            }
            /// <summary>
            /// 显示进度条
            /// </summary>
            private void InvokePrgBar()
            {
                if (prgBar.Value < 100)
                {
                    prgBar.Value = 100 * (int)m_downedFileSize / m_AllFileSize;
                    prgBar.Update();
                    System.Diagnostics.Debug.WriteLine("prgBar.Value:{0}" + 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(100);
                    MethodInvoker mi = new MethodInvoker(this.InvokePrgBar);
                    this.BeginInvoke(mi);
                } while (m_AllFileSize <= m_downedFileSize);
            }
            #endregion

            /// <summary>
            /// 关闭窗体时关闭线程
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmAutoUpdate_FormClosing(object sender, FormClosingEventArgs e)
            {
                try
                {
                    DownThread.Abort();
                }
                catch
                {
                    ;
                }
            }
        }
    }


 


[C语言系列]C# 过滤html,js,css代码 正则表达式  [C语言系列]C# DataGridView显示行号的两种方法
[C语言系列]C# WinForm 中Label自动换行 解决方法  [C语言系列]C# 线程调用主线程中的控件
[电脑应用]c# winform 打包部署 自定义界面 或设置开机启动  [C语言系列]C# 和 Linux 时间戳转换
[C语言系列]C#实现 WebBrowser中新窗口打开链接用默认或者指定…  [C语言系列]C#全角和半角转换
[C语言系列]c#WebBrowser查找并选择文本  [C语言系列]C#中实现WebBrowser控件的HTML源代码读写
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台