转载 升级程序的制作方法

介绍了一种用Web Services制作升级程序的方法,通过Web Services升级程序如同读写本机文件般简单,并给出了Web Services部分代码,代码转载自相关网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      收藏  原帖地址 : http://blog.youkuaiyun.com/chendazhi/archive/2006/05/09/715542.aspx

升级程序的制作有多种方法,下面介绍一种用Web Services制作的升级程序。通过Web Services 升级程序就象读写本机文件一样简单。所以我就直接给出代码。

 

Web Services部分代码:


None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
using  System.Web.Services.Protocols;
None.gif
using  System.IO;
None.gif
None.gif
None.gif[WebService(Namespace 
=   " http://tempuri.org/ " )]
None.gif[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
None.gif
public   class  Service : System.Web.Services.WebService
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public Service()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//如果使用设计的组件,请取消注释以下行
InBlock.gif         
//InitializeComponent();
ExpandedSubBlockEnd.gif
    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 需要升级文件的服务器路径
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    private const string UpdateServerPath ="d:\\Debug";
InBlock.gif
InBlock.gif    [WebMethod(Description 
= "返回服务器上程序的版本号")]
InBlock.gif    
public string ServerVer()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "4.0";
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [WebMethod(Description 
= "返回需更新的文件")]
InBlock.gif    
public string[] NewFiles()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DirectoryInfo di 
= new DirectoryInfo(UpdateServerPath);
InBlock.gif        FileInfo[] fi 
= di.GetFiles();
InBlock.gif        
int intFiles= fi.Length;
InBlock.gif        
string[] myNewFiles = new string[intFiles];
InBlock.gif        
int i = 0;
InBlock.gif        
foreach (FileInfo fiTemp in fi)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            myNewFiles[i] 
= fiTemp.Name;
InBlock.gif            System.Diagnostics.Debug.WriteLine(fiTemp.Name);
InBlock.gif            i
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return myNewFiles;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [WebMethod(Description 
= "返回需更新的文件的大小")]
InBlock.gif    
public int AllFileSize()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int filesize = 0;
InBlock.gif        
string[] files = Directory.GetFiles(UpdateServerPath);
InBlock.gif        
foreach (string file in files)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FileInfo myInfo 
= new FileInfo(file);
InBlock.gif            filesize 
+= (int)myInfo.Length / 1024;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return filesize;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [WebMethod(Description 
= "返回给定文件的字节数组")]
InBlock.gif    
public byte[] GetNewFile(string requestFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////得到服务器端的一个文件
InBlock.gif        if (requestFileName != null || requestFileName != "")
InBlock.gif            
return getBinaryFile(UpdateServerPath + "\\"+requestFileName);
InBlock.gif        
else
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 返回所给文件路径的字节数组。
InBlock.gif     
/// </summary>
InBlock.gif    
/// <param name="filename"></param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    private byte[] getBinaryFile(string filename)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (File.Exists(filename))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//打开现有文件以进行读取。
InBlock.gif

InBlock.gif                FileStream s 
= File.OpenRead(filename);
InBlock.gif                
return ConvertStreamToByteBuffer(s);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new byte[0];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new byte[0];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 把给定的文件流转换为二进制字节数组。
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="theStream"></param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

InBlock.gif    private byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int b1;
InBlock.gif        System.IO.MemoryStream tempStream 
= new System.IO.MemoryStream();
InBlock.gif        
while ((b1 = theStream.ReadByte()) != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tempStream.WriteByte(((
byte)b1));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return tempStream.ToArray();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

升级程序代码:
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Threading;
None.gif
using System.Xml;
None.gif
using System.IO;
None.gif
using System.Diagnostics;
None.gif
None.gif
namespace AutoUpdate
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class frmAutoUpdate : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public frmAutoUpdate()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前版本
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string m_strLocalVer;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 服务器版本
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string m_strServerVer;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 需要更新的文件总数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int m_intFilesCount = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 需要更新的文件大小
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        
private int m_AllFileSize;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 正在下载的文件大小
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        
private int m_downingFileSize;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 已经下载的文件大小
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        
private int m_downedFileSize;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///数组,需要更新的文件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        
private string[] m_Files = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定义文件对象
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif
InBlock.gif        WebReference.Service ws;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 连接WebServeces并下载文件线程
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private Thread DownThread;
InBlock.gif        
private void frmAutoUpdata_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DownThread 
= new Thread(new ThreadStart(StartUpdate));
InBlock.gif            DownThread.Start();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 连接服务器,开始升级
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void StartUpdate()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ws 
= new WebReference.Service();
InBlock.gif            m_AllFileSize 
= ws.AllFileSize();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_strLocalVer 
=Config.Ver; //从配置文件中读取当前版本号
InBlock.gif
                m_strServerVer = ws.ServerVer();//保存服务器版本号
InBlock.gif
                BeginInvoke(new System.EventHandler(UpdateUI), lblLink);
InBlock.gif
InBlock.gif                
//当前版本低于服务器版本,开始更新dot.gif
InBlock.gif
                if (double.Parse(m_strLocalVer) < double.Parse(m_strServerVer))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    BeginInvoke(
new System.EventHandler(UpdateUI), lblDownLoad);
InBlock.gif                    m_Files 
= ws.NewFiles();//需要更新的文件
InBlock.gif
                    if (m_Files != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        m_intFilesCount 
= m_Files.Length;
InBlock.gif                        
for (int i = 0; i <= m_intFilesCount - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            DownFile(m_Files[i]);
InBlock.gif                            Debug.WriteLine(m_Files[i]);
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
// Config.VER = strServerVer;//将版本号记录到配置文件中
ExpandedSubBlockEnd.gif
                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    BeginInvoke(
new EventHandler(OnShowMessage),
InBlock.gif                        
"你计算机上安装的已经是最新版本了,不需要升级.");
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BeginInvoke(
new EventHandler(OnShowMessage), ex.Message);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//UI线程设置label属性
InBlock.gif
            BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
InBlock.gif
InBlock.gif            
//安装文件
InBlock.gif
            MethodInvoker miSetup = new MethodInvoker(this.StartSetup);
InBlock.gif            
this.BeginInvoke(miSetup);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 下载文件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="fileName">文件名</param>

InBlock.gif        private void DownFile(string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//得到二进制文件字节数组;
InBlock.gif
            byte[] bt = ws.GetNewFile(fileName);
InBlock.gif            m_downingFileSize 
= bt.Length / 1024;
InBlock.gif            
string strPath = Application.StartupPath + "\\Update\\" + fileName;
InBlock.gif            
if (File.Exists(strPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                File.Delete(strPath);
ExpandedSubBlockEnd.gif            }

InBlock.gif            FileStream fs 
= new FileStream(strPath, FileMode.CreateNew);
InBlock.gif            fs.Write(bt, 
0, bt.Length);
InBlock.gif            fs.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 开始安装下载的文件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif       private void StartSetup()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 结束当前运行的主程序
InBlock.gif
            Process[] pros = Process.GetProcesses();
InBlock.gif            
for (int i = 0; i < pros.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (pros[i].ProcessName == "教师考勤系统")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    pros[i].Kill();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 开始复制文件
InBlock.gif
            for (int i = 0; i <= m_intFilesCount - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string sourceFileName = Application.StartupPath + "\\Update\\" + m_Files[i];
InBlock.gif                
string destFileName = Application.StartupPath + "\\" + m_Files[i];
InBlock.gif                
if (File.Exists(destFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.Delete(destFileName);
ExpandedSubBlockEnd.gif                }

InBlock.gif                File.Copy(sourceFileName, destFileName, 
false);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 升级完成,启动主程序
InBlock.gif
            string StrExe = Application.StartupPath + "\\教师考勤系统.exe";
InBlock.gif
InBlock.gif            
if (File.Exists(StrExe))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Diagnostics.Process.Start(StrExe);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//关闭升级程序
InBlock.gif
            Application.Exit();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
辅助方法,确保线程安全#region 辅助方法,确保线程安全
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Label上显示信息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void UpdateUI(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label lbl 
= (Label)sender;
InBlock.gif            lbl.ForeColor 
= SystemColors.Desktop;
InBlock.gif            lblLocalVer.Text 
= m_strLocalVer;
InBlock.gif            lblServerVer.Text 
= m_strServerVer;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 显示对话框
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void OnShowMessage(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MessageBox.Show(
this, sender.ToString(), this.Text,
InBlock.gif                MessageBoxButtons.OK, MessageBoxIcon.Information);
InBlock.gif
InBlock.gif            Thread.Sleep(
100);
InBlock.gif            
this.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 显示进度条
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InvokePrgBar()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (prgBar.Value < 100)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                prgBar.Value 
= 100 * (int)m_downedFileSize / m_AllFileSize;
InBlock.gif                prgBar.Update();
InBlock.gif                System.Diagnostics.Debug.WriteLine(
"prgBar.Value:{0}" + prgBar.Value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 计算文件已下载的百分比
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void subPrgBar()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_downedFileSize 
+= m_downingFileSize;
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"AllFileSize  " + m_AllFileSize);
InBlock.gif            System.Diagnostics.Debug.WriteLine(
"downingFileSize  " + m_downingFileSize);
InBlock.gif            
do
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Thread.Sleep(
100);
InBlock.gif                MethodInvoker mi 
= new MethodInvoker(this.InvokePrgBar);
InBlock.gif                
this.BeginInvoke(mi);
ExpandedSubBlockEnd.gif            }
 while (m_AllFileSize <= m_downedFileSize);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 关闭窗体时关闭线程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        private void frmAutoUpdate_FormClosing(object sender, FormClosingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DownThread.Abort();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


在创建Web服务时要把位置改为:文件系统(默认为HTTP)

转载于:https://www.cnblogs.com/shineboy1219/archive/2007/07/04/805190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值