- 更新程序和主程序是分开的,得在做一个exe可执行更新程序。
- 主程序在登陆时判断是否需要更新。
我这边判断方式是直接在配置文件里面设置版本号,然后和服务器上面的版本对比,低于服务器版本就更新程序。
//该文件配置在app.config里面
<!-- 发布前修改版本号 -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="version" value="1.1.6" />
</appSettings>
</configuration>
获取配置文件信息,前几章随笔里面有提到。
Version now_v = new Version(strval);//当前版本
Version load_v = new Version(model.version.ToString());//历史版本
if (now_v < load_v && MessageBox.Show("检查到新版本,是否更新?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//更新(调用更新的exe)返回新版本路径
string fileName = Application.StartupPath + @"ClientUpdate.exe";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = fileName;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = now_v + " " + load_v + " " + cn.showVersion()+" "+ model.download.ToString();//参数以空格分隔,如果某个参数为空,可以传入””
p.Start();
System.Environment.Exit(System.Environment.ExitCode); //结束主线程
}
3.接下来是更新程序的代码部分

private void Form1_Load(object sender, EventArgs e)
{
try
{
String[] CmdArgs = System.Environment.GetCommandLineArgs();
if (CmdArgs.Length > 1)
{
//参数0是它本身的路径
String arg0 = CmdArgs[0].ToString();//程序路径
String arg1 = CmdArgs[1].ToString();//旧版本号
String arg2 = CmdArgs[2].ToString();//新版本号
String arg3 = CmdArgs[3].ToString();//更新版本
String arg4 = CmdArgs[4].ToString();//版本地址
//更新
lab_version.Text = arg2;
this.Text = arg3 + "更新";
UpdateDownLoad(arg4);//
listBox1.Items.Add("更新信息");
foreach (var item in CmdArgs)
{
listBox1.Items.Add(item.ToString());
}
label6.Text = "完成更新";
button1.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 更新
/// </summary>
/// <param name="verStr"></param>
private void UpdateDownLoad(string verStr)
{
WebClient wc = new WebClient();
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileAsync(new Uri(verStr), "../"+ZipHelper.zipFileFullName);//要下载文件的路径,下载之后的命名
}
void wc_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
Action act = () =>
{
this.progressBar1.Value = e.ProgressPercentage;
this.label1.Text = e.ProgressPercentage + "%";
};
this.Invoke(act);
if (e.ProgressPercentage == 100)
{
//下载完成之后开始覆盖
ZipHelper.Unzip();//调用解压的类
this.button1.Enabled = true;
}
}
public delegate void ChangeBarDel(System.Net.DownloadProgressChangedEventArgs e);
/// <summary>
/// 完成更新之后再次打开主程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
this.Close();
string fileName = Application.StartupPath + @"GoldenTax.exe";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = fileName;
p.StartInfo.CreateNoWindow = true;
p.Start();
System.Environment.Exit(System.Environment.ExitCode); //结束主线程
}
}
4.解压类
class ZipHelper
{
public static string zipFileFullName = "Update.rar";
public static void Unzip()
{
try
{
string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
string s7z = _appPath + @"7-Zip7z.exe";
System.Diagnostics.Process pNew = new System.Diagnostics.Process();
pNew.StartInfo.FileName = s7z;
pNew.StartInfo.Arguments = string.Format(" x "{0}\{1}" -y -o"{0}"", _appPath + "/../", zipFileFullName);
//x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
pNew.Start();
//等待完成
pNew.WaitForExit();
//删除压缩包
File.Delete(_appPath + @"/../" + zipFileFullName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}