[.NET源码] C#制作的一套在线更新软件系统

这个不算是第一个版本了,却是一个比较完善的版本,四个月之前我曾将其带到Writebok2.1版本,本打算着为新云翻译器2.2带来一个比较好的更新系统,但是由于时间问题,新云翻译器2.2仓促发布,也没能用上这套系统。这两天有点时间就完善了这套更新系统,感觉还不错。

使用的是C#语言,框架是.NET FrameWork 4.0,编程工具是Visual Studio2015

  1. ?using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Xml;
  10. using System.Threading;
  11. http://www.kmnk03.com/hxpfk/xmz/409.html
  12. namespace SoftwareUpdate
  13. {
  14. class InitializationUpdate
  15. {
  16. #region 一些对象和变量
  17. //使用WebClient下载
  18. public WebClient client = new WebClient();
  19. public ArrayList downlist = new ArrayList();
  20. //当前版本
  21. public string localversion = null;
  22. //最新版本
  23. public string latesversion = null;
  24. //通知内容
  25. public string nnidtext = null;
  26. #endregionhttp://www.kmnk03.com/hxpfk/xmz/410.html
  27. public void Initialization()
  28. {
  29. //获取本地版本号
  30. NowVersion();
  31. //下载最新版本号
  32. DownloadCheckUpdateXml();
  33. //获取最新版本号
  34. LatestVersion();
  35. //下载安装包
  36. DownloadInstall();
  37. }
  38. /// <summary>
  39. /// 获取本地软件的版本号
  40. /// </summary>http://www.kmnk03.com/hxpfk/xmz/411.html
  41. public void NowVersion()
  42. {
  43. FileVersionInfo fv = FileVersionInfo.GetVersionInfo("新云翻译器.exe");
  44. localversion = fv.FileVersion;
  45. }
  46. /// <summary>
  47. /// 从服务器上获取最新的版本号
  48. /// </summary>
  49. public void DownloadCheckUpdateXml()
  50. {
  51. try
  52. {http://www.kmnk03.com/hxpfk/xmz/412.html
  53. //第一个参数是文件的地址,第二个参数是文件保存的路径文件名
  54. client.DownloadFile("http://cloudyours.net/Software/Update/NewcloudTranslator/NewcloudTranslator222.XML", @"Update\NewcloudTranslator222.XML");
  55. }
  56. catch
  57. {
  58. nnidtext = "没有检测到更新";
  59. //Environment.Exit(0);
  60. }
  61. }
  62. /// <summary>
  63. /// 读取从服务器获取的最新版本号
  64. /// </summary>
  65. public void LatestVersion()
  66. {
  67. if (File.Exists(@"Update\NewcloudTranslator222"))
  68. {
  69. latesversion = "2.2.2.0";
  70. }
  71. else if (!File.Exists(@"Update\NewcloudTranslator222"))
  72. {http://www.kmnk03.com/hxpfk/xmz/413.html
  73. nnidtext = "检查更新失败";
  74. //Environment.Exit(0);
  75. }
  76. }
  77. /// <summary>
  78. /// 下载安装包
  79. /// </summary>
  80. public void DownloadInstall()
  81. {
  82. if (localversion == latesversion)
  83. {
  84. nnidtext = "恭喜你,已经更新到最新版本";http://www.kmnk01.com/hxpfk/2015/py_1229/381.html
  85. }
  86. else if (localversion != latesversion && File.Exists(@"Update\NewcloudTranslator222"))
  87. {
  88. nnidtext = "发现新版本,即将下载更新补丁";
  89. client.DownloadFile("http://cloudyours.net/Software/Update/NewcloudTranslator/NewCloudTranslator2_2_2_Setup.exe", @"Update\NewCloudTranslator2_2_1_210_Setup.exe");
  90. if (File.Exists(@"Update\NewCloudTranslator2_2_2_Setup.exe"))
  91. {
  92. InstallProgram ip = new InstallProgram();
  93. ip.InstallandDelete();
  94. }
  95. else if (!File.Exists(@"Update\NewCloudTranslator2_2_2_Setup.exe"))
  96. {http://www.kmnk01.com/hxpfk/2015/py_1229/382.html
  97. //如果一次没有下载成功,则检查三次
  98. for (int i = 1; i < 3; i++)
  99. {
  100. client.DownloadFile("http://cloudtours.net/Software/Update/NewcloudTranslator/NewCloudTranslator2_2_2_Setup.exe", @"Update\NewCloudTranslator2_2_2_Setup.exe");
  101. }
  102. nnidtext = "下载失败,请检查您的网络连接是否正常";
  103. //Environment.Exit(0);
  104. }
  105. }
  106. }
  107. }
  108. }
复制代码
  1. ?using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.IO;
  7. namespace SoftwareUpdate
  8. {
  9. class InstallProgram
  10. {
  11. /// <summary>
  12. /// 安装及删除
  13. /// </summary>
  14. public void InstallandDelete()
  15. {
  16. //安装前关闭正在运行的程序
  17. KillProgram();
  18. //启动安装程序http://www.kmnk01.com/hxpfk/2015/py_1229/383.html
  19. Process.Start(@"Update\NewCloudTranslator2_2_2_Setup.exe");
  20. JudgeInstall();
  21. }
  22. /// <summary>
  23. /// 判断安装进程是否存在
  24. /// </summary>
  25. public void JudgeInstall()
  26. {
  27. Process[] processList = Process.GetProcesses();
  28. foreach (Process process in processList)
  29. {
  30. if (process.ProcessName == "NewCloudTranslator2_2_2_Setup.exe")
  31. {
  32. process.Kill();
  33. File.Delete(@"Update\NewCloudTranslator2_2_2_Setup.exe");
  34. File.Delete(@"Update\NewcloudTranslator222");
  35. }
  36. else
  37. {
  38. File.Delete(@"Update\NewCloudTranslator2_2_2_Setup.exe");
  39. File.Delete(@"Update\NewcloudTranslator222");
  40. return;
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 结束程序
  46. /// </summary>
  47. public void KillProgram()
  48. {
  49. Process[] processList = Process.GetProcesses();
  50. foreach (Process process in processList)
  51. {http://www.kmnk01.com/hxpfk/2015/py_1229/384.html
  52. //如果程序启动了,则杀死
  53. if (process.ProcessName == "新云翻译器.exe")
  54. {
  55. process.Kill();
  56. }
  57. }
  58. }
  59. }
  60. }
复制代码
  1. ?using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace SoftwareUpdate
  13. {
  14. public partial class MainForm : Form
  15. {
  16. public MainForm()
  17. {
  18. InitializeComponent();
  19. }
  20. #region 移动窗体API
  21. [System.Runtime.InteropServices.DllImport("user32.dll")]
  22. public static extern bool ReleaseCapture();
  23. [System.Runtime.InteropServices.DllImport("user32.dll")]
  24. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  25. public const int WM_SYSCOMMAND = 0x0112;
  26. public const int SC_MOVE = 0xF010;
  27. public const int HTCAPTION = 0x0002;
  28. #endregionhttp://www.kmnk01.com/hxpfk/2015/py_1229/385.html
  29. //调用更新的类
  30. InitializationUpdate su = new InitializationUpdate();
  31. /// <summary>
  32. /// 程序运行时
  33. /// </summary>
  34. /// <param name="sender"></param>
  35. /// <param name="e"></param>
  36. private void Form1_Load(object sender, EventArgs e)
  37. {
  38. //取消检查跨线程操作
  39. CheckForIllegalCrossThreadCalls = false;
  40. //程序启动时赋予他们最新的版本号
  41. su.NowVersion();
  42. LocalVersion.Text = su.localversion;
  43. Thread td = new Thread(Judge);
  44. td.Start();
  45. }
  46. /// <summary>
  47. /// 监视NNID的变化
  48. /// </summary>
  49. public void Judge()
  50. {
  51. try
  52. {
  53. while (true)
  54. {
  55. labNNID.Text = su.nnidtext;
  56. //因为结束进程的办法导致此处抛异常,不过并不会影响程序
  57. }
  58. }
  59. catch { }
  60. }
  61. /// <summary>
  62. /// 点击关闭窗体
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. ///
  67. private void formclose_Click(object sender, EventArgs e)
  68. {
  69. this.Close();
  70. }
  71. /// <summary>
  72. /// 点击检查更新
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. private void btnUpdate_Click(object sender, EventArgs e)
  77. {
  78. try
  79. {
  80. su.DownloadCheckUpdateXml();
  81. }
  82. catch { labNNID.Text = "没有检测到更新"; }
  83. try
  84. {
  85. su.LatestVersion();
  86. }
  87. catch { labNNID.Text = "检查更新失败"; }
  88. try
  89. {
  90. su.DownloadInstall();
  91. }
  92. catch { labNNID.Text = "下载失败,请检查您的网络连接是否正常"; }
  93. }
  94. /// <summary>
  95. /// 当鼠标移动时
  96. /// </summary>
  97. /// <param name="sender"></param>
  98. /// <param name="e"></param>
  99. private void Form1_MouseDown_1(object sender, MouseEventArgs e)
  100. {
  101. ReleaseCapture();
  102. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  103. }
  104. }
  105. }kmnk01.com
    kmnk03.com
    www.kmnk01.com
    www.kmnk03.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值