< type="text/JavaScript"> < src="http://a.alimama.cn/inf.js" type="text/javascript">
现在但凡是一个程序都有相应的升级程序,如果你的程序没有相应的升级程序,那么你就需要留意了。你的用户很可能丢失!!!网上关于自动升级的例子也有很多,前几天一个朋友很苦恼的跟我说它的客户在逐渐减少(据他所说,他都客户因为他的程序升级很麻烦,所以很多人放弃了使用它的软件),问我说怎么办?其实他也知道该怎么办?所以...朋友嘛!就给他做了一个自动升级程序。恰好今天优快云上的一位老友也询问这件事情,所以就把代码共享大家了。
先个几个图:
主要原理(相当简单):
升级程序一定要是一个单独的exe,最好不要和你的程序绑到一起(否则升级程序无法启动)。主程序退出----升级程序启动----升级程序访问你的网站的升级配置文件-----读取配置文件里面信息-----下载-----升级程序关闭----主程序启动
主要代码:
1.读取配置文件:
view plaincopy to clipboardprint?
private void GetUpdateInfo()
{
//获取服务器信息
WebClient client = new WebClient();
doc = new XmlDocument();
try
{
doc.Load(client.OpenRead("http://192.168.3.43/update/update.xml"));
//doc.Load(client.OpenRead(Config.IniReadValue("Update","UpdateURL",Application.StartupPath+"//config.ini")+"//update.xml"));
//doc.Load(Application.StartupPath + "//update.xml");
client = null;
}
catch
{
this.labHtml.Text = "无法取得更新文件!程序升级失败!";
return;
}
if (doc == null)
return;
//分析文件
XmlNode node;
//获取文件列表
string RootPath = doc.SelectSingleNode("Product/FileRootPath").InnerText.Trim();
node = doc.SelectSingleNode("Product/FileList");
if (node != null)
{
foreach (XmlNode xn in node.ChildNodes)
{
this.listView1.Items.Add(new ListViewItem(new string[]
{
xn.Attributes["Name"].Value.ToString(),
new WebFileInfo(RootPath+xn.Attributes["Name"].Value.ToString()).GetFileSize().ToString(),
"---"
}));
}
}
}
private void GetUpdateInfo()
{
//获取服务器信息
WebClient client = new WebClient();
doc = new XmlDocument();
try
{
doc.Load(client.OpenRead("http://192.168.3.43/update/update.xml"));
//doc.Load(client.OpenRead(Config.IniReadValue("Update","UpdateURL",Application.StartupPath+"//config.ini")+"//update.xml"));
//doc.Load(Application.StartupPath + "//update.xml");
client = null;
}
catch
{
this.labHtml.Text = "无法取得更新文件!程序升级失败!";
return;
}
if (doc == null)
return;
//分析文件
XmlNode node;
//获取文件列表
string RootPath = doc.SelectSingleNode("Product/FileRootPath").InnerText.Trim();
node = doc.SelectSingleNode("Product/FileList");
if (node != null)
{
foreach (XmlNode xn in node.ChildNodes)
{
this.listView1.Items.Add(new ListViewItem(new string[]
{
xn.Attributes["Name"].Value.ToString(),
new WebFileInfo(RootPath+xn.Attributes["Name"].Value.ToString()).GetFileSize().ToString(),
"---"
}));
}
}
}
2.文件下载:
view plaincopy to clipboardprint?
/// <summary>
/// 下载文件
/// </summary>
public void Download()
{
FileStream fs = new FileStream( this.strFile,FileMode.Create,FileAccess.Write,FileShare.ReadWrite );
try
{
this.objWebRequest = (HttpWebRequest)WebRequest.Create( this.strUrl );
this.objWebRequest.AllowAutoRedirect = true;
// int nOffset = 0;
long nCount = 0;
byte[] buffer = new byte[ 4096 ]; //4KB
int nRecv = 0; //接收到的字节数
this.objWebResponse = (HttpWebResponse)this.objWebRequest.GetResponse();
Stream recvStream = this.objWebResponse.GetResponseStream();
long nMaxLength = (int)this.objWebResponse.ContentLength;
if( this.bCheckFileSize && nMaxLength != this.nFileSize )
{
throw new Exception( string.Format( "文件/"{0}/"被损坏,无法下载!",Path.GetFileName( this.strFile ) ) );
}
if( this.DownloadFileStart != null )
this.DownloadFileStart( new DownloadFileStartEventArgs( (int)nMaxLength ) );
while( true )
{
nRecv = recvStream.Read( buffer,0,buffer.Length );
if( nRecv == 0 )
break;
fs.Write( buffer,0,nRecv );
nCount += nRecv;
//引发下载块完成事件
if( this.DownloadFileBlock != null )
this.DownloadFileBlock( new DownloadFileEventArgs( (int)nMaxLength,(int)nCount ) );
}
recvStream.Close();
//引发下载完成事件
if( this.DownloadFileComplete != null )
this.DownloadFileComplete( this,EventArgs.Empty );
}
finally
{
fs.Close();
}
}
/// <summary>
/// 下载文件
/// </summary>
public void Download()
{
FileStream fs = new FileStream( this.strFile,FileMode.Create,FileAccess.Write,FileShare.ReadWrite );
try
{
this.objWebRequest = (HttpWebRequest)WebRequest.Create( this.strUrl );
this.objWebRequest.AllowAutoRedirect = true;
// int nOffset = 0;
long nCount = 0;
byte[] buffer = new byte[ 4096 ]; //4KB
int nRecv = 0; //接收到的字节数
this.objWebResponse = (HttpWebResponse)this.objWebRequest.GetResponse();
Stream recvStream = this.objWebResponse.GetResponseStream();
long nMaxLength = (int)this.objWebResponse.ContentLength;
if( this.bCheckFileSize && nMaxLength != this.nFileSize )
{
throw new Exception( string.Format( "文件/"{0}/"被损坏,无法下载!",Path.GetFileName( this.strFile ) ) );
}
if( this.DownloadFileStart != null )
this.DownloadFileStart( new DownloadFileStartEventArgs( (int)nMaxLength ) );
while( true )
{
nRecv = recvStream.Read( buffer,0,buffer.Length );
if( nRecv == 0 )
break;
fs.Write( buffer,0,nRecv );
nCount += nRecv;
//引发下载块完成事件
if( this.DownloadFileBlock != null )
this.DownloadFileBlock( new DownloadFileEventArgs( (int)nMaxLength,(int)nCount ) );
}
recvStream.Close();
//引发下载完成事件
if( this.DownloadFileComplete != null )
this.DownloadFileComplete( this,EventArgs.Empty );
}
finally
{
fs.Close();
}
}
欢迎转载,转载请注明出处:)
< type="text/JavaScript"> < src="http://a.alimama.cn/inf.js" type="text/javascript">
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/gisfarmer/archive/2009/08/12/4437994.aspx