呵呵,恰巧在实际应用中做过,说下我的方法:
1. 有一个固定IP地址的数据库服务器。数据库中存放一个软件版本表,有版本字段,下载路径字段,更新时间等(楼主的是在文件里,我的是写在库里的)
2. 发布的程序中标识一个版本号,添加一个资源,这个资源是升级程序,在program.cs中加一个检查升级的方法:
private static void CheckUpdate()
{
//升级程序名
string UpdateFile = "SystemUpdate.exe";
if (("rar.exe"))
("rar.exe");
if (("Compress.rar"))
("Compress.rar");
if ((UpdateFile))
(UpdateFile);//释放升级程序前删除现有的升级文件
UpdateService.Service1 Updater = new UpdateService.Service1();//这是个WEB服务,放在固定IP的服务器上
int newver = int.Parse(Updater.GetNewVersion());//从服务器获取最新版本号
if (newver > version)//与当前系统版本做对比
{
IsNeedUpdate = true;//需要升级
byte[] bytUpdate = resUpdate.SystemUpdate;//读取升级程序字节流
//创建升级程序文件写入器
fs = new (UpdateFile,
,
);
//二进制写文件
bWriter = new (fs);
//输出内容到升级程序
bWriter.Write(bytUpdate, 0, bytUpdate.Length);
bWriter.Close();
fs.Close();
fs.Dispose();
//开启一个进程执行升级程序
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = UpdateFile;
proc.Start();
proc.Close();
proc.Dispose();
//退出当前程序
Environment.Exit(Environment.ExitCode);
}
}
3.升级程序。同要添加一个资源,里面放rar.exe,这个程序在WINRAR里就有,我是把所有程序中用的都打成一个包,包括DLL和BIN文件,还可以设置密码
下面是升级方法的主要内容:
private void BegionUpdate()
{
lblTips.Text = "删除当前版的系统……";
string currentshell = Process.GetCurrentProcess().MainModule.FileName;
string path = currentshell.Substring(0, currentshell.LastIndexOf(@"\"));
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles();
List<string> lstFileToBeDeleted = new List<string>();
Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
lblTips.Text = "收集文件……";
pgs.Maximum = files.Length - 1;
int iValue = 0;
foreach (FileInfo file in files)
{
pgs.Value = iValue++;
if (currentshell != file.FullName)
{
string ExtName = file.Name.Substring(file.Name.LastIndexOf(".")).ToUpper();
string MainName = file.Name.Substring(0, file.Name.LastIndexOf("."));
List<string> lstExt = new List<string>();
if (!dic.ContainsKey(MainName))
{
lstExt.Add(ExtName);
dic.Add(MainName, lstExt);
}
else
{
lstExt = dic[MainName];
if (!lstExt.Contains(ExtName))
lstExt.Add(ExtName);
dic[MainName] = lstExt;
}
if (!lstFileToBeDeleted.Contains(file.Name))
lstFileToBeDeleted.Add(file.Name);
}
}
lblTips.Text = "检查系统中是否有平台相关进程在运行……";
Process[] execute = Process.GetProcesses();
pgs.Maximum = execute.Length - 1;
iValue = 0;
foreach (Process proc in execute)
{
pgs.Value = iValue++;
if (dic.ContainsKey(proc.ProcessName))
{
if (dic[proc.ProcessName].Contains(".EXE"))
proc.Kill();
}
}
if (lstFileToBeDeleted.Count > 0)
{
lblTips.Text = "删除旧版本的系统……";
pgs.Maximum = lstFileToBeDeleted.Count - 1;
iValue = 0;
foreach (string delFile in lstFileToBeDeleted)
{
try
{
pgs.Value = iValue++;
File.Delete(delFile);
}
catch
{
MessageBox.Show("删除" + delFile + "失败,请手工删除\r\n否则可能导致升级失败");
continue;
}
}
}
lblTips.Text = "获取系统升级信息";
RemoteData.Service1 rmtService = new RemoteData.Service1();//从这行起的4行代码最好调用WebService,用Remoting可能被防火墙拦下
string url = rmtService.GetDownloadURL();//
string date = rmtService.GetNewVersionDate();//
string times = rmtService.GetNewVersion();//
lblTips.Text = "正在下载" + date + "第" + times + "次更新";
HTMLPAGE downloader = new HTMLPAGE();//这是个封闭的类,只用到下载功能,大家可以自己实现
downloader.Download(url, "Compress.rar");
try
{
lblTips.Text = "开始升级系统……";
//clsWinrar rar = new clsWinrar();
//rar.unCompressRAR(path, path, "Compress.rar");
string rarTool = "rar.exe";
byte[] bytUpdate = resRAR.Rar;//读取升级程序字节流
//创建升级程序文件写入器
fs = new (rarTool,
,
);
//二进制写文件
bWriter = new (fs);
//输出内容到升级程序
bWriter.Write(bytUpdate, 0, bytUpdate.Length);
bWriter.Close();
fs.Close();
fs.Dispose();
//开启一个进程执行升级程序
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo srtInfo = new ProcessStartInfo();
srtInfo.FileName = rarTool;
srtInfo.Arguments = " x Compress.rar";
srtInfo.CreateNoWindow = true;
srtInfo.UseShellExecute = true;
srtInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo = srtInfo;
proc.Start();
try
{
while (System.Diagnostics.Process.GetProcessById(proc.Id) != null)
Application.DoEvents();
}
catch (Exception err)
{
if (err.Message.IndexOf("进程当前未运行") != -1)
Application.DoEvents();
}
proc.Close();
proc.Dispose();
Process system = new Process();
system.StartInfo.FileName = "发布程序启动的可执行文件名.exe";
system.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
system.StartInfo.CreateNoWindow = false;
system.Start();
//system.Close();
//system.Dispose();
}
catch
{
MessageBox.Show("成功下载升级包,请手工解压运行");
}
this.Close();
}