| 网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 |
![]() |
| Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道 |
面介绍一种用Web Services制作的升级程序。通过Web Services 升级程序就象读写本机文件一样简单。所以我就直接给出代码。 网管联盟bitsCN_com
Web Services部分代码:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO; 网管u家u.bitsCN.com
[WebService(Namespace = "http://tempuri.org/")]
[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 = "返回服务器上程序的版本号")]
网管u家u.bitscn@com
网管u家u.bitscn@com
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;
} 网管下载dl.bitscn.com
return filesize;
}
[WebMethod(Description = "返回给定文件的字节数组")]
public byte[] GetNewFile(string requestFileName)
{
///得到服务器端的一个文件
if (requestFileName != null || requestFileName != "")
return getBinaryFile(UpdateServerPath + "\\"+requestFileName);
else
return null;
} 网管网www.bitscn.com
/// <summary>
/// 返回所给文件路径的字节数组。
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private byte[] getBinaryFile(string filename)
{
if (File.Exists(filename))
{
try
{
//打开现有文件以进行读取。 网管u家u.bitscn@com
FileStream s = File.OpenRead(filename);
return ConvertStreamToByteBuffer(s);
}
catch
{
return new byte[0];
}
}
else
{ 网管网www.bitscn.com
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)
} 网管u家u.bitsCN.com
升级程序代码:
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;
网管网www_bitscn_com
namespace AutoUpdate
{
public partial class frmAutoUpdate : Form
{
public frmAutoUpdate()
{
InitializeComponent();
}
/// <summary>
/// 当前版本
/// </summary>
private string m_strLocalVer;
/// <summary>
/// 服务器版本
网管网www.bitscn.com
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();//需要更新的文件
网管论坛bbs_bitsCN_com
if (File.Exists(StrExe))
{
System.Diagnostics.Process.Start(StrExe);
}
//关闭升级程序
Application.Exit();
}
网管网www_bitscn_com
#region 辅助方法,确保线程安全 网管bitscn_com
/// <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;
} 网管bitscn_com
/// <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();
} 中国网管联盟bitsCN.com
/// <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); 中国网管论坛bbs.bitsCN.com
}
}
/// <summary>
/// 计算文件已下载的百分比
/// </summary>
private void subPrgBar()
{
m_downedFileSize += m_downingFileSize;
System.Diagnostics.Debug.WriteLine("AllFileSize " + m_AllFileSize);
System.Diagnostics.Debug.WriteLine("downingFileSize " + m_downingFileSize);
中国网管论坛bbs.bitsCN.com
/// <summary>
/// 关闭窗体时关闭线程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmAutoUpdate_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
DownThread.Abort();
|
0
|
评论加载中…