DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO
数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr
国外测试服务器: http://h31bt.com 大家可以给提点意见...
--------------------------------------------------------------------------------------------------------------------
关于自动更新,在.NET下面已经是很普通的事情,无非就是在服务器端保存配置好要更新的程序,然后客户端再写一个小程序来检测,有更新的则复制过来。
但现在问题是就一个程序,如何让程序自己进行更新而不用调用另外的程序,对于用户来说体验更好.
1.首先程序exe自己下载覆盖自己肯定是不行的,会报“当前程序正在被另一个进程所使用”的系统错误; 2.在进程中的程序不能覆盖自己,但是可以重命名,你从远程下载一个新的exe文件,重命名为xxx.exe.tmp; 3.待下载完毕后,把旧的exe重命名一下,比如xxx.exe.old,然后把新的xxx.exe.tmp重命名为xxx.exe; 4.重命名的方法:System.IO.File.Move(filepath + ".tmp", filepath); 5.然后重启程序Application.Restart();在form_Load事件里面判断一下 if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"xxx.exe.old")) System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"xxx.exe.old"); 这样就可以了,如果做的再好点的话,可以把xxx.exe.old的文件属性设置为隐藏就好了.
网上搜索的方法:
如果建一个bat文件,执行的是复制功能,然后再用一个bat来调用他,并且用一个bat文件去杀掉某一个文件,然后再复制新的,再启动是没有什么问题的吧。
添加了一个方法KillSelfThenRun()用于删除正在运行的主Exe,然后再重启新的主Exe。代码全部粘贴如下:
KillSelfThenRun
第三步调用时,原程序本身就有一个Try的做法,就在Catch里面判断一下,如果出现IOException,就调用这个方法。
点击完成复制更新文件到应用程序目录
先看使用方法:
在主程序界面里面增加一个定时器,然后用来延时来判断服务器上的程序是否有更新,如果在程序一开始就判断,程序会加载很慢,从而给用户体验不好.
string m_localPath = AppDomain.CurrentDomain.BaseDirectory; string thisexename = Application.ExecutablePath.Replace(m_localPath, ""); H31Updater update1 = new H31Updater(); bool isupdate = update1.CheckServerUpdate(m_localPath, thisexename); if (isupdate) { DialogResult dlg = MessageBox.Show(this, "检测到服务器上有新版本,需要更新么?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dlg == DialogResult.OK) { update1.Show(); update1.UpdaterStart(); } }
然后程序就会显示更新进度条:
程序更新后将自己命令为*.old,然后使用MOVE命令将新的更新为自己.
服务器上的返回XML文件列表方式:
<?xml version="1.0" encoding="utf-8" ?> <AutoUpdater> <UpdateInfo id="1"> <Version value = "1.0.0.3"/> </UpdateInfo> <!--升级文件列表--> <UpdateFileList> <UpdateFile>software\H31Thunder.exe</UpdateFile>
<!--可以复制多行,更新自己目录下面的程序-->
<UpdateFile>software\H31Thunder.exe</UpdateFile>
</UpdateFileList> </AutoUpdater>
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Globalization; using System.IO; using System.Net; using System.Xml; using System.Threading; namespace H31Thunder { public partial class H31Updater : Form { private WebClient downWebClient = new WebClient(); private static string m_exePath = ""; private static string m_exeName = ""; private static string m_url = "http://h31bt.com/"; private static string[] fileNames; private static int m_downNowPos;//已更新文件数 private static string fileName;//当前文件名 public H31Updater() { InitializeComponent(); } public bool CheckServerUpdate(string exePath,string exeName) { m_exePath = exePath; m_exeName = exeName; if (File.Exists(m_exePath + m_exeName + ".old")) File.Delete(m_exePath + m_exeName + ".old"); string currentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); string theNewVersion = GetTheNewVersion(m_url + "version.asp?id=1"); if (!String.IsNullOrEmpty(theNewVersion) && !String.IsNullOrEmpty(currentVersion)) { if (string.Compare(theNewVersion,currentVersion)> 0) { return true; } } return false; } /// <summary> /// 开始更新 /// </summary> public void UpdaterStart() { try { float tempf; //委托下载数据时事件 this.downWebClient.DownloadProgressChanged += delegate(object wcsender, DownloadProgressChangedEventArgs ex) { this.label2.Text = String.Format(CultureInfo.InvariantCulture,"正在下载:{0} [ {1}/{2} ]",fileName,ConvertSize(ex.BytesReceived),ConvertSize(ex.TotalBytesToReceive)); tempf = ((float)(m_downNowPos) / fileNames.Length); this.progressBar2.Value = ex.ProgressPercentage; this.progressBar1.Value = Convert.ToInt32(tempf * 90); }; //委托下载完成时事件 this.downWebClient.DownloadFileCompleted += delegate(object wcsender, AsyncCompletedEventArgs ex) { if (ex.Error != null) { MeBox(ex.Error.Message); } else { if (fileNames.Length > m_downNowPos) { DownloadFile(m_downNowPos); } else { this.progressBar1.Value = this.progressBar1.Maximum; this.progressBar2.Value = this.progressBar2.Maximum; Thread.Sleep(100); UpdaterClose(); } } }; m_downNowPos = 0; if (fileNames != null) DownloadFile(0); } catch (WebException ex) { MeBox(ex.Message); } } /// <summary> /// 下载文件 /// </summary> /// <param name="arry">下载序号</param> private void DownloadFile(int arry) { try { m_downNowPos++; int findpos = fileNames[arry].LastIndexOf("/"); if (findpos != -1) fileName = fileNames[arry].Substring(findpos+1, fileNames[arry].Length - findpos-1); else fileName = fileNames[arry]; this.label1.Text = String.Format(CultureInfo.InvariantCulture, "更新进度 {0}/{1}", m_downNowPos, fileNames.Length); this.progressBar2.Value = 0; string weburl = m_url + fileNames[arry]; string savename=m_exePath + fileName + ".new"; this.downWebClient.DownloadFileAsync(new Uri(weburl), savename); } catch (Exception ex) { MeBox(ex.Message); } } /// <summary> /// 关闭程序 /// </summary> private static void UpdaterClose() { try { if (File.Exists(m_exePath + m_exeName + ".old")) File.Delete(m_exePath + m_exeName + ".old"); for(int i=0;i<fileNames.Length;i++) { int findpos = fileNames[i].LastIndexOf("/"); string savename = fileNames[i]; if(findpos!=-1) savename = fileNames[i].Substring(findpos+1, fileNames[i].Length - findpos-1); if (File.Exists(m_exePath + savename + ".old")) File.Delete(m_exePath + savename + ".old"); File.Move(m_exePath + savename, m_exePath + savename + ".old"); File.Move(m_exePath + savename + ".new", m_exePath + savename); File.SetAttributes(m_exePath + savename + ".old", FileAttributes.Archive | FileAttributes.Hidden); } } catch (Exception ex) { //if (ex.GetType() == typeof(IOException)) //{ //} //else MeBox(ex.Message); } Application.Exit(); } /// <summary> /// 判断软件的更新日期 /// </summary> /// <param name="Dir">服务器地址</param> /// <returns>返回日期</returns> private static string GetTheNewVersion(string webUrl) { string NewVersion = ""; try { WebClient wc = new WebClient(); Stream getStream = wc.OpenRead(webUrl); StreamReader streamReader = new StreamReader(getStream, Encoding.UTF8); string xmltext = streamReader.ReadToEnd(); streamReader.Close(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.InnerXml = xmltext; XmlNode root = xmlDoc.SelectSingleNode("//Version"); if (root.Name == "Version") { NewVersion = root.Attributes["value"].Value.ToString(); } XmlNodeList root1 = xmlDoc.SelectNodes("//UpdateFileList"); if (root1 != null) { fileNames = new string[root1.Count]; int i=0; foreach (XmlNode temp in root1) { fileNames[i] = temp.InnerText.Replace("\\","/"); i++; } } } catch (WebException ex) { //MeBox(ex.Message); } return NewVersion; } /// <summary> /// 弹出提示框 /// </summary> /// <param name="txt">输入提示信息</param> private static void MeBox(string txt) { MessageBox.Show(txt, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } /// <summary> /// 转换字节大小 /// </summary> /// <param name="byteSize">输入字节数</param> /// <returns>返回值</returns> private static string ConvertSize(long byteSize) { string str = ""; float tempf = (float)byteSize; if (tempf / 1024 > 1) { if ((tempf / 1024) / 1024 > 1) { str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB"; } else { str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB"; } } else { str = tempf.ToString(CultureInfo.InvariantCulture) + "B"; } return str; } } }程序自动更新类代码
如果大家有什么更好的方法,请指教.谢谢.
下载程序:H31Thunder.rar
下一篇给大家介绍上面图中显示如何从迅雷服务器上偷用视频图片地址的方法.不信的话大家可以去查看下图片的 http://h31bt.com网址是不是自己服务器上的...
当然希望大家多多推荐哦...大家的推荐才是下一篇介绍的动力...