看看这段代码。。。 private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
WebClient webClient = new WebClient();
webClient.DownloadFile("http://localhost/Web/update.txt", "update.txt"); StreamReader sr = new StreamReader("update.txt"); List<UpdateFile> files = new List<UpdateFile>(); string version = sr.ReadLine();
//将应用程序更名拷贝
File.Copy(@"MyGame.exe", "MyGame_old.exe", true);
//反射备份的应用程序 得到版本号
Assembly assembly = Assembly.LoadFile(Application.StartupPath + @"\MyGame_old.exe");
string currentVersion = assembly.GetName().Version.ToString();
assembly = null;
//如果版本号不一致 升级程序
if (currentVersion != version)
{
//读出有几个文件需要更新
int newFileCount = int.Parse(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("升级成功!");
}
else
{
MessageBox.Show("您的游戏已经是最新版本!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} /// <summary>
/// 保存要更新的文件名和下载路径
/// </summary>
public class UpdateFile
{
private string fileName;
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
private string downLoadPath;
public string DownLoadPath
{
get { return downLoadPath; }
set { downLoadPath = value; }
} } |