c#

位置:IT落伍者 >> c# >> 浏览文章

c#在线升级


发布日期:2018年08月22日
 
c#在线升级

面介绍一种用Web Services制作的升级程序通过Web Services 升级程序就象读写本机文件一样简单所以我就直接给出代码

Web Services部分代码

using System;

using SystemWeb;

using SystemWebServices;

using SystemWebServicesProtocols;

using SystemIO;

[WebService(Namespace = )]

[WebServiceBinding(ConformsTo = WsiProfilesBasicProfile_)]

public class Service : SystemWebServicesWebService

{

public Service()

{

//如果使用设计的组件请取消注释以下行

//InitializeComponent();

}

/// <summary>

/// 需要升级文件的服务器路径

/// </summary>

private const string UpdateServerPath =d:\\Debug;

[WebMethod(Description = 返回服务器上程序的版本号)]

public string ServerVer()

{

return ;

}

[WebMethod(Description = 返回需更新的文件)]

public string[] NewFiles()

{

DirectoryInfo di = new DirectoryInfo(UpdateServerPath);

FileInfo[] fi = diGetFiles();

int intFiles= fiLength;

string[] myNewFiles = new string[intFiles];

int i = ;

foreach (FileInfo fiTemp in fi)

{

myNewFiles[i] = fiTempName;

SystemDiagnosticsDebugWriteLine(fiTempName);

i++;

}

return myNewFiles;

}

[WebMethod(Description = 返回需更新的文件的大小)]

public int AllFileSize()

{

int filesize = ;

string[] files = DirectoryGetFiles(UpdateServerPath);

foreach (string file in files)

{

FileInfo myInfo = new FileInfo(file);

filesize += (int)myInfoLength / ;

}

return filesize;

}

[WebMethod(Description = 返回给定文件的字节数组)]

public byte[] GetNewFile(string requestFileName)

{

///得到服务器端的一个文件

if (requestFileName != null || requestFileName != )

return getBinaryFile(UpdateServerPath + \\+requestFileName);

else

return null;

}

/// <summary>

/// 返回所给文件路径的字节数组

/// </summary>

/// <param name=filename></param>

/// <returns></returns>

private byte[] getBinaryFile(string filename)

{

if (FileExists(filename))

{

try

{

//打开现有文件以进行读取

FileStream s = FileOpenRead(filename);

return ConvertStreamToByteBuffer(s);

}

catch

{

return new byte[];

}

}

else

{

return new byte[];

}

}

/// <summary>

/// 把给定的文件流转换为二进制字节数组

/// </summary>

/// <param name=theStream></param>

/// <returns></returns>

private byte[] ConvertStreamToByteBuffer(SystemIOStream theStream)

{

int b;

SystemIOMemoryStream tempStream = new SystemIOMemoryStream();

while ((b = theStreamReadByte()) != )

{

tempStreamWriteByte(((byte)b));

}

return tempStreamToArray();

}

}

升级程序代码

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

using SystemThreading;

using SystemXml;

using SystemIO;

using SystemDiagnostics;

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>

WebReferenceService ws;

/// <summary>

/// 连接WebServeces并下载文件线程

/// </summary>

private Thread DownThread;

private void frmAutoUpdata_Load(object sender EventArgs e)

{

DownThread = new Thread(new ThreadStart(StartUpdate));

DownThreadStart();

}

/// <summary>

/// 连接服务器开始升级

/// </summary>

private void StartUpdate()

{

ws = new WebReferenceService();

m_AllFileSize = wsAllFileSize();

try

{

m_strLocalVer =ConfigVer; //从配置文件中读取当前版本号

m_strServerVer = wsServerVer();//保存服务器版本号

BeginInvoke(new SystemEventHandler(UpdateUI) lblLink);

//当前版本低于服务器版本开始更新

if (doubleParse(m_strLocalVer) < doubleParse(m_strServerVer))

{

BeginInvoke(new SystemEventHandler(UpdateUI) lblDownLoad);

m_Files = wsNewFiles();//需要更新的文件

if (m_Files != null)

{

m_intFilesCount = m_FilesLength;

for (int i = ; i <= m_intFilesCount ; i++)

{

DownFile(m_Files[i]);

DebugWriteLine(m_Files[i]);

}

// ConfigVER = strServerVer;//将版本号记录到配置文件中

}

}

else

{

BeginInvoke(new EventHandler(OnShowMessage)

你计算机上安装的已经是最新版本了不需要升级);

return;

}

}

catch (Exception ex)

{

BeginInvoke(new EventHandler(OnShowMessage) exMessage);

return;

}

//UI线程设置label属性

BeginInvoke(new SystemEventHandler(UpdateUI) lblStartExe);

//安装文件

MethodInvoker miSetup = new MethodInvoker(thisStartSetup);

thisBeginInvoke(miSetup);

}

/// <summary>

/// 下载文件

/// </summary>

/// <param name=fileName>文件名</param>

private void DownFile(string fileName)

{

//得到二进制文件字节数组

byte[] bt = wsGetNewFile(fileName);

m_downingFileSize = btLength / ;

string strPath = ApplicationStartupPath + \\Update\\ + fileName;

if (FileExists(strPath))

{

FileDelete(strPath);

}

FileStream fs = new FileStream(strPath FileModeCreateNew);

fsWrite(bt btLength);

fsClose();

}

/// <summary>

/// 开始安装下载的文件

/// </summary>

private void StartSetup()

{

// 结束当前运行的主程序

Process[] pros = ProcessGetProcesses();

for (int i = ; i < prosLength; i++)

{

if (pros[i]ProcessName == 教师考勤系统)

{

pros[i]Kill();

}

}

// 开始复制文件

for (int i = ; i <= m_intFilesCount ; i++)

{

string sourceFileName = ApplicationStartupPath + \\Update\\ + m_Files[i];

string destFileName = ApplicationStartupPath + \\ + m_Files[i];

if (FileExists(destFileName))

{

FileDelete(destFileName);

}

FileCopy(sourceFileName destFileName false);

}

// 升级完成启动主程序

string StrExe = ApplicationStartupPath + \\教师考勤系统exe;

if (FileExists(StrExe))

{

SystemDiagnosticsProcessStart(StrExe);

}

//关闭升级程序

ApplicationExit();

}

#region 辅助方法确保线程安全

/// <summary>

/// Label上显示信息

/// </summary>

private void UpdateUI(object sender EventArgs e)

{

Label lbl = (Label)sender;

lblForeColor = SystemColorsDesktop;

lblLocalVerText = m_strLocalVer;

lblServerVerText = m_strServerVer;

}

/// <summary>

/// 显示对话框

/// </summary>

private void OnShowMessage(object sender EventArgs e)

{

MessageBoxShow(this senderToString() thisText

MessageBoxButtonsOK MessageBoxIconInformation);

ThreadSleep();

thisClose();

}

/// <summary>

/// 显示进度条

/// </summary>

private void InvokePrgBar()

{

if (prgBarValue < )

{

prgBarValue = * (int)m_downedFileSize / m_AllFileSize;

prgBarUpdate();

SystemDiagnosticsDebugWriteLine(prgBarValue:{} + prgBarValue);

}

}

/// <summary>

/// 计算文件已下载的百分比

/// </summary>

private void subPrgBar()

{

m_downedFileSize += m_downingFileSize;

SystemDiagnosticsDebugWriteLine(AllFileSize + m_AllFileSize);

SystemDiagnosticsDebugWriteLine(downingFileSize + m_downingFileSize);

do

{

ThreadSleep();

MethodInvoker mi = new MethodInvoker(thisInvokePrgBar);

thisBeginInvoke(mi);

} while (m_AllFileSize <= m_downedFileSize);

}

#endregion

/// <summary>

/// 关闭窗体时关闭线程

/// </summary>

/// <param name=sender></param>

/// <param name=e></param>

private void frmAutoUpdate_FormClosing(object sender FormClosingEventArgs e)

{

try

{

DownThreadAbort();

}

catch

{

;

}

}

}

}

               

上一篇:C# 2010命名和可选参数的新特性

下一篇:C# 语言规范--1.1 开始