面介紹一種用Web Services制作的升級程序
Web Services部分代碼
using System;
using System
using System
using System
using System
[WebService(Namespace =
[WebServiceBinding(ConformsTo = WsiProfiles
public class Service : System
{
public Service()
{
//如果使用設計的組件
//InitializeComponent();
}
/// <summary>
/// 需要升級文件的服務器路徑
/// </summary>
private const string UpdateServerPath =
[WebMethod(Description =
public string ServerVer()
{
return
}
[WebMethod(Description =
public string[] NewFiles()
{
DirectoryInfo di = new DirectoryInfo(UpdateServerPath);
FileInfo[] fi = di
int intFiles= fi
string[] myNewFiles = new string[intFiles];
int i =
foreach (FileInfo fiTemp in fi)
{
myNewFiles[i] = fiTemp
System
i++;
}
return myNewFiles;
}
[WebMethod(Description =
public int AllFileSize()
{
int filesize =
string[] files = Directory
foreach (string file in files)
{
FileInfo myInfo = new FileInfo(file);
filesize += (int)myInfo
}
return filesize;
}
[WebMethod(Description =
public byte[] GetNewFile(string requestFileName)
{
///得到服務器端的一個文件
if (requestFileName != null || requestFileName !=
return getBinaryFile(UpdateServerPath +
else
return null;
}
/// <summary>
/// 返回所給文件路徑的字節數組
/// </summary>
/// <param name=
/// <returns></returns>
private byte[] getBinaryFile(string filename)
{
if (File
{
try
{
//打開現有文件以進行讀取
FileStream s = File
return ConvertStreamToByteBuffer(s);
}
catch
{
return new byte[
}
}
else
{
return new byte[
}
}
/// <summary>
/// 把給定的文件流轉換為二進制字節數組
/// </summary>
/// <param name=
/// <returns></returns>
private byte[] ConvertStreamToByteBuffer(System
{
int b
System
while ((b
{
tempStream
}
return tempStream
}
}
升級程序代碼
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
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 =
/// <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
/// <summary>
/// 連接WebServeces並下載文件線程
/// </summary>
private Thread DownThread;
private void frmAutoUpdata_Load(object sender
{
DownThread = new Thread(new ThreadStart(StartUpdate));
DownThread
}
/// <summary>
/// 連接服務器
/// </summary>
private void StartUpdate()
{
ws = new WebReference
m_AllFileSize = ws
try
{
m_strLocalVer =Config
m_strServerVer = ws
BeginInvoke(new System
//當前版本低於服務器版本
if (double
{
BeginInvoke(new System
m_Files = ws
if (m_Files != null)
{
m_intFilesCount = m_Files
for (int i =
{
DownFile(m_Files[i]);
Debug
}
// Config
}
}
else
{
BeginInvoke(new EventHandler(OnShowMessage)
return;
}
}
catch (Exception ex)
{
BeginInvoke(new EventHandler(OnShowMessage)
return;
}
//UI線程設置label屬性
BeginInvoke(new System
//安裝文件
MethodInvoker miSetup = new MethodInvoker(this
this
}
/// <summary>
/// 下載文件
/// </summary>
/// <param name=
private void DownFile(string fileName)
{
//得到二進制文件字節數組
byte[] bt = ws
m_downingFileSize = bt
string strPath = Application
if (File
{
File
}
FileStream fs = new FileStream(strPath
fs
fs
}
/// <summary>
/// 開始安裝下載的文件
/// </summary>
private void StartSetup()
{
// 結束當前運行的主程序
Process[] pros = Process
for (int i =
{
if (pros[i]
{
pros[i]
}
}
// 開始復制文件
for (int i =
{
string sourceFileName = Application
string destFileName = Application
if (File
{
File
}
File
}
// 升級完成
string StrExe = Application
if (File
{
System
}
//關閉升級程序
Application
}
#region 輔助方法
/// <summary>
/// Label上顯示信息
/// </summary>
private void UpdateUI(object sender
{
Label lbl = (Label)sender;
lbl
lblLocalVer
lblServerVer
}
/// <summary>
/// 顯示對話框
/// </summary>
private void OnShowMessage(object sender
{
MessageBox
MessageBoxButtons
Thread
this
}
/// <summary>
/// 顯示進度條
/// </summary>
private void InvokePrgBar()
{
if (prgBar
{
prgBar
prgBar
System
}
}
/// <summary>
/// 計算文件已下載的百分比
/// </summary>
private void subPrgBar()
{
m_downedFileSize += m_downingFileSize;
System
System
do
{
Thread
MethodInvoker mi = new MethodInvoker(this
this
} while (m_AllFileSize <= m_downedFileSize);
}
#endregion
/// <summary>
/// 關閉窗體時關閉線程
/// </summary>
/// <param name=
/// <param name=
private void frmAutoUpdate_FormClosing(object sender
{
try
{
DownThread
}
catch
{
;
}
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11273.html