日期:2014-05-17  浏览次数:20855 次

c# 升级程序怎么做?? 思路??
如题?
没做过这方面的...

望高手 指点下...

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Reflection;

namespace Update
{
public partial class UpdateFomr : Form
{
public UpdateFomr()
{
InitializeComponent();
}

/// <summary>
/// 升级按扭事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
WebClient webClient = new WebClient();

//指定路径的文件下载到当前目录下创建名为:update.txt
webClient.DownloadFile(@"F:\Game\Mir2\UpdateFile\update.txt", "update.txt");

StreamReader sr = new StreamReader("update.txt");

List<UpdateFile> files = new List<UpdateFile>();

//升级文件的版本号
string version = sr.ReadLine();

//MessageBox.Show(version);

//将应用程序更名复制
File.Copy(@"Game.exe", "Game_Old.exe", true);

//反射备份的应用程序 得到版本号
Assembly assembly = Assembly.LoadFile(Application.StartupPath
+ @"\Game.exe");

//程序原来的版本号
string currentVersion = assembly.GetName().Version.ToString();

assembly = null;

//如果版本不一致,则升级程序
if (currentVersion != version)
{

//读出有几个文件需要更新
int newFileCount = Convert.ToInt32(sr.ReadLine());

for (int i = 0; i < newFileCount; i++)
{
//取得文件下载路径和文件名
UpdateFile updateFile = new UpdateFile();
updateFile.FileName = sr.ReadLine();
updateFile.DownLoadPath = sr.ReadLine();
files.Add(updateFile);
}

foreach (UpdateFile file in files)
{
WebClient webClientNew = new WebClient();
//更新每个文件 - 注意web服务器的配置
webClientNew.DownloadFile(file.DownLoadPath, file.FileName);
}
MessageBox.Show("升级成功!");

Application.Exit(); //退出更新程序窗体

System.Diagnostics.Process.Start("Game.exe"); //打开游戏窗体可执行文件
}
else
{
MessageBox.Show("您的游戏已经是最新版本!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// 不升级按扭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeUpdate_Click(object sender, EventArgs e)
{
Application.Exit(); //退出更新程序窗体

System.Diagnostics.Process.Start("Game.exe"); //打开游戏窗体
}
}
}
------解决方案----