在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#网络编程 → C# 自动升级程序源码

C# 自动升级程序源码

C#网络编程

下载此实例
  • 开发语言:C#
  • 实例大小:0.08M
  • 下载次数:181
  • 浏览次数:1729
  • 发布时间:2013-08-29
  • 实例类别:C#网络编程
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# 升级

实例介绍

【实例简介】

【实例截图】

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace UpdateTools
{
    public partial class FrmUpdateTool : Form
    {


        #region Fileds
        public string curVer = "1.0.0.0";
        public string auditor = "tanrh";
        private string path = "http://localhost/";
        private string appName = "Main.exe";
        XmlDocument xDoc = new XmlDocument();
        #endregion

        #region Constructor
        public FrmUpdateTool()
        {
            InitializeComponent();
        }
        #endregion

        #region Internal Methods
        private string BuildUpdateConfigFile(string path)
        {
            string tab = "\t";
            StringBuilder sb = new StringBuilder(1500);
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.AppendLine("<updater>");

            sb.Append(tab);
            sb.AppendLine(string.Format("<description>{0}</description>", "update"));
            sb.AppendLine();

            sb.Append(tab);
            string updateUrl = String.Format("{0}{1}/", "http://localhost/", Path.GetFileName(path));
            sb.AppendLine(String.Format("<updateUrls defaultUrl=\"{0}\" >",updateUrl));
            sb.Append(tab).Append(tab);
            sb.AppendLine(String.Format("<updateUrl>{0}</updateUrl>", updateUrl));
            sb.Append(tab);
            sb.AppendLine("</updateUrls>");
            sb.AppendLine();

            sb.Append(tab);
            sb.AppendLine(String.Format("<application ver=\"{0}\" path=\"{1}\" lastUpdate=\"{2}\" >", curVer, path, DateTime.Now.ToString()));
            sb.Append(tab).Append(tab).AppendLine(appName);
            sb.Append(tab);
            sb.AppendLine("</application>");
            sb.AppendLine();
            
            //生成目录
            sb.AppendLine(BuildUpdateConfigFile(path, "\t"));

            sb.AppendLine();
            sb.Append(tab);
            sb.AppendLine(String.Format("<updateLog updateTime=\"{0}\">",DateTime.Now.ToString()));
            sb.Append(tab).Append(tab);
            sb.AppendLine("<files>");
            sb.Append(tab).Append(tab).Append(tab);
            sb.AppendLine(String.Format("<file name=\"{0}\" path=\"{1}\" ver=\"{2}\" error=\"\" />", String.Empty, String.Empty, String.Empty));
            sb.Append(tab).Append(tab);
            sb.AppendLine("</files>");
            sb.Append(tab);
            sb.AppendLine("</updateLog>");

            sb.AppendLine("</updater>");

            return sb.ToString();
        }

        private string BuildUpdateConfigFile(string path, string tab)
        {
            string[] files = Directory.GetFiles(path);
            StringBuilder sb = new StringBuilder(100);
            string pathName = path.Substring(path.LastIndexOf("\\")   1);
            string dirTab = tab;
            sb.Append(dirTab);
            sb.AppendLine(String.Format("<dir name=\"{0}\" lastUpdate=\"{1}\" ver=\"{2}\" >", pathName, Directory.GetLastWriteTimeUtc(path), curVer));
            tab = string.Concat(tab, "\t");
            for (int i = 0; i < files.Length; i  )
            {
                try
                {
                    FileInfo fileInfo = new FileInfo(files[i]);
                    sb.Append(tab);
                    sb.AppendFormat("<file lastUpdate=\"{0}\" ver=\"{1}\" auditor=\"{2}\" name=\"{3}\" />", fileInfo.LastWriteTimeUtc.ToLocalTime(), curVer, auditor, fileInfo.Name);
                    sb.AppendLine();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }

            string[] dirs = Directory.GetDirectories(path);
            for (int j = 0; j < dirs.Length; j  )
            {
                sb.AppendLine(BuildUpdateConfigFile(dirs[j],tab));
            }
            sb.Append(dirTab);
            sb.AppendFormat("</dir>");
            return sb.ToString();

        }

        private void SaveConfigureFile(string fileName)
        {
            FileStream fs = null;
            try
            {
                fs = File.Open(fileName, FileMode.Create, FileAccess.Write);
                TextWriter tw= new StreamWriter(fs, Encoding.UTF8);
                tw.Write(rtbConfigFile.Text);
                tw.Flush();
                tw.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                    fs = null;
                }
            }
        }

        private void  LoadDirectoryFiles(TreeNode dirNode,XmlNode xNode)
        {
            XmlNodeList xNodeList = xNode.SelectNodes("file");
            foreach (XmlNode oNode in xNodeList)
            {
                string fileName = oNode.Attributes["name"] != null ? oNode.Attributes["name"].Value : String.Empty;
                if(!String.IsNullOrEmpty(fileName))
                {
                    TreeNode fileNode = new TreeNode();
                    fileNode.Text = fileName;
                    fileNode.Tag = oNode;
                    fileNode.ImageKey = "file2";
                    fileNode.SelectedImageKey = "file2";
                    dirNode.Nodes.Add(fileNode);
                }
            }

            xNodeList = xNode.SelectNodes("dir");
            foreach (XmlNode oNode in xNodeList)
            {
                string directory = oNode.Attributes["name"] != null ? oNode.Attributes["name"].Value : "文件夹";
                TreeNode subDirNode = new TreeNode(directory);
                subDirNode.Tag = oNode;
                subDirNode.ImageKey = "directory";
                subDirNode.SelectedImageKey = "directory";
                dirNode.Nodes.Add(subDirNode);
                LoadDirectoryFiles(subDirNode, oNode);
            }

        }

        private void LoadUpdateConfigureFile(TreeView tv, string text)
        {
            xDoc.LoadXml(text);

            XmlNode rootNode = xDoc.DocumentElement;

            XmlNode xNode = rootNode.SelectSingleNode("description");
            TreeNode tNode = new TreeNode("描述信息");
            tNode.ImageKey = "file2";
            tNode.SelectedImageKey = "file2";
            tNode.Tag = xNode.InnerText;
            tv.Nodes.Add(tNode);

            xNode = rootNode.SelectSingleNode("updateUrls");
            tNode = new TreeNode("服务器列表");
            tNode.ImageKey = "select";
            tNode.SelectedImageKey = "select";
            tv.Nodes.Add(tNode);
            tNode.Tag = xNode.Attributes["defaultUrl"] != null ? xNode.Attributes["defaultUrl"].Value : "";
            XmlNodeList xNodelist = xNode.SelectNodes("updateUrl");
            foreach (XmlNode oNode in xNodelist)
            {
                if (String.IsNullOrEmpty(oNode.InnerText))
                {
                    TreeNode urlNode = new TreeNode(oNode.InnerText);
                    urlNode.Tag = oNode;
                    urlNode.ImageKey = "urlfile";
                    urlNode.SelectedImageKey = "urlfile";
                    tNode.Nodes.Add(urlNode);
                }
            }

            xNode = rootNode.SelectSingleNode("application");
            tNode = new TreeNode("应用程序");
            tNode.ImageKey = "directory";
            tNode.SelectedImageKey = "directory";
            tNode.Tag = xNode;
            tv.Nodes.Add(tNode);
            TreeNode appNode = new TreeNode(xNode.InnerText);
            appNode.SelectedImageKey = "application";
            appNode.ImageKey = "application";
            tNode.Nodes.Add(appNode);

            xNode = rootNode.SelectSingleNode("dir");
            string appDirectory = xNode.Attributes["name"] != null ? xNode.Attributes["name"].Value : "主程序文件夹";
            TreeNode tAppDirNode = new TreeNode(appDirectory);
            tAppDirNode.ImageKey = "directory";
            tAppDirNode.SelectedImageKey = "directory";
            tAppDirNode.Tag = xNode;
            tv.Nodes.Add(tAppDirNode);

            LoadDirectoryFiles(tAppDirNode, xNode);

            xNode = rootNode.SelectSingleNode("updateLog");
            TreeNode logNode = new TreeNode("更新日志");
            logNode.ImageKey = "directory";
            logNode.SelectedImageKey  = "directory";
            logNode.Tag = xNode;
            tv.Nodes.Add(logNode);
            xNodelist = xNode.SelectNodes("files/file");
            foreach (XmlNode oNode in xNodelist)
            {
                if (oNode.Attributes["name"] != null && String.IsNullOrEmpty(oNode.Attributes["name"].Value))
                {
                    TreeNode logfileNode = new TreeNode(oNode.Attributes["name"].Value);
                    logfileNode.ImageKey = "file1";
                    logfileNode.SelectedImageKey = "file1";
                    logNode.Nodes.Add(logfileNode);
                }
            }
        }
           #endregion

        #region EventHandler
        private void btnBuild_Click(object sender, EventArgs e)
        {
            string selectPath = string.Empty;
            FolderBrowserDialog fdDlg = new FolderBrowserDialog();
            if (fdDlg.ShowDialog() == DialogResult.OK)
            {
                selectPath = fdDlg.SelectedPath;
                if (String.IsNullOrEmpty(selectPath)) return;

                rtbConfigFile.Text = BuildUpdateConfigFile(selectPath);   

                LoadUpdateConfigureFile(tvFiles,rtbConfigFile.Text);
             
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.FileName = "update";
            saveDlg.Filter = "(*.xml)|*.xml";
            if(saveDlg.ShowDialog() == DialogResult.OK)
            {
                SaveConfigureFile(saveDlg.FileName);
            }
        }
        #endregion
    }
}

标签: C# 升级

网友评论

第 1 楼 橡树 发表于: 2016-04-26 15:52 30
怎么运行不了呢?请问是在什么vs版本上运行?谢谢

支持(0) 盖楼(回复)

第 2 楼 crazycode 发表于: 2016-04-27 10:07 57
怎么运行不了呢?请问是在什么vs版本上运行?谢谢

橡树 2016-04-26 15:52 30

vs2008 或者 vs2010, 如果你的版本较高,可参考其逻辑

支持(0) 盖楼(回复)

第 3 楼 shiy720 发表于: 2016-06-29 19:35 22
....

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有3条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警