您的位置:程序门 -> .net技术 -> c#



c# 软件升级程序制作


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c# 软件升级程序制作[已结贴,结贴人:hwb_net]
发表于:2007-03-17 09:18:39 楼主
如果做完一个软件后,要进行更新的话,要如何升级程序??
发表于:2007-03-17 09:23:151楼 得分:0
mark..
发表于:2007-03-17 09:36:242楼 得分:25
mark
以下是我写的程序很垃圾,参考一下吧。


using   system;
using   system.drawing;
using   system.collections;
using   system.componentmodel;
using   system.windows.forms;
using   system.data;
using   textandini.ini;
using   textandini.text;    
using   system.net;  
using   system.io;
using   system.text;        
using   system.threading   ;
using   system.runtime.interopservices;  
namespace   更新程序
{
///   <summary>
///   form1   的摘要说明。
///   </summary>
public   class   form1   :   system.windows.forms.form
{
[dllimport( "user32.dll ")]      
private     static       extern     unsafe   intptr   findwindow(string   lpclassname,string   lpwindowname);
[dllimport( "user32.dll ")]  
private   static   extern   unsafe   int   sendmessage(intptr   hwnd,   int   msg,intptr   wparam,string   lparam   );  
private     const   int   wm_close   =   0x10;             //关闭程序。
private   string   windowname;   //主程序窗口名称。
private   arraylist   filelist1;   //现有文件列表。
private   arraylist   filelist2;   //下载到的文件列表。
private   arraylist   filesize;   //文件大小。
private   arraylist   updatalist;   //要更新的文件列表。
private   string         hurl;   //下载的网址。
private   intptr   hwnd;   //窗口句柄。
private   thread   th;   //进程1。
private   thread   thread;   //进程2。
private   mytext   textlog;
//*******************************************************************************************
private   system.windows.forms.progressbar   progressbar1;
private   system.windows.forms.groupbox   groupbox1;
private   system.windows.forms.label   label1;
private   system.windows.forms.timer   timer1;
private   system.windows.forms.listview   listview1;
private   system.windows.forms.statusbar   statusbar1;
private   system.componentmodel.icontainer   components;

public   form1()
{
//
//   windows   窗体设计器支持所必需的
//
initializecomponent();

//
//   todo:   在   initializecomponent   调用后添加任何构造函数代码
//
windowname= " ";
string   data   =   string.format( "{0}年{1}月{2}日 ",datetime.now.year,datetime.now.month,datetime.now.day);    
textlog   =   new   mytext(application.startuppath+ "\\log\\ "+data+ ".log ");  
textlog.writetext( "**************************************************************************\r\n ");  
textlog.writetext( "日期: "+datetime.now.tostring()+ "程序启动\r\n ");  
textlog.writetext( "**************************************************************************\r\n ");  

}

///   <summary>
///   清理所有正在使用的资源。
///   </summary>
protected   override   void   dispose(   bool   disposing   )
{
if(   disposing   )
{
if   (components   !=   null)  
{
components.dispose();
}
}
base.dispose(   disposing   );
}

#region   windows   窗体设计器生成的代码
///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   initializecomponent()
{
this.components   =   new   system.componentmodel.container();
this.progressbar1   =   new   system.windows.forms.progressbar();
this.groupbox1   =   new   system.windows.forms.groupbox();
this.listview1   =   new   system.windows.forms.listview();
this.label1   =   new   system.windows.forms.label();
this.timer1   =   new   system.windows.forms.timer(this.components);
this.statusbar1   =   new   system.windows.forms.statusbar();
this.groupbox1.suspendlayout();
this.suspendlayout();
//  
//   progressbar1
//  
this.progressbar1.location   =   new   system.drawing.point(24,   232);
this.progressbar1.name   =   "progressbar1 ";
this.progressbar1.size   =   new   system.drawing.size(336,   16);
this.progressbar1.tabindex   =   0;
//  
//   groupbox1
//  
this.groupbox1.controls.add(this.listview1);
this.groupbox1.location   =   new   system.drawing.point(8,   8);
this.groupbox1.name   =   "groupbox1 ";
this.groupbox1.size   =   new   system.drawing.size(376,   176);
this.groupbox1.tabindex   =   3;
this.groupbox1.tabstop   =   false;
this.groupbox1.text   =   "测试服务器状态 ";
//  
//   listview1
//  
this.listview1.location   =   new   system.drawing.point(8,   24);
this.listview1.name   =   "listview1 ";
this.listview1.size   =   new   system.drawing.size(360,   144);
this.listview1.tabindex   =   0;
this.listview1.view   =   system.windows.forms.view.details;
//  
//   label1
//  
this.label1.location   =   new   system.drawing.point(32,   200);
this.label1.name   =   "label1 ";
this.label1.size   =   new   system.drawing.size(328,   24);
this.label1.tabindex   =   4;
//  
//   timer1
//  
this.timer1.tick   +=   new   system.eventhandler(this.timer1_tick);
//  
//   statusbar1
//  
this.statusbar1.location   =   new   system.drawing.point(0,   261);
this.statusbar1.name   =   "statusbar1 ";
this.statusbar1.size   =   new   system.drawing.size(392,   16);
this.statusbar1.tabindex   =   5;
//  
//   form1
//  
this.autoscalebasesize   =   new   system.drawing.size(6,   14);
this.clientsize   =   new   system.drawing.size(392,   277);
this.controls.add(this.statusbar1);
this.controls.add(this.label1);
this.controls.add(this.groupbox1);
this.controls.add(this.progressbar1);
this.formborderstyle   =   system.windows.forms.formborderstyle.fixedtoolwindow;
this.maximizebox   =   false;
this.name   =   "form1 ";
this.startposition   =   system.windows.forms.formstartposition.centerscreen;
this.text   =   "万博软件更新程序 ";
this.closing   +=   new   system.componentmodel.canceleventhandler(this.form1_closing);
this.load   +=   new   system.eventhandler(this.form1_load);
this.groupbox1.resumelayout(false);
this.resumelayout(false);

}
#endregion

///   <summary>
///   应用程序的主入口点。
///   </summary>
发表于:2007-03-17 09:36:413楼 得分:0
[stathread]
static   void   main()  
{
application.run(new   form1());
}

private   void   form1_load(object   sender,   system.eventargs   e)
{
try
{
hwnd   =   new   intptr(0);
filelist1   =   new   arraylist();  
streamreader   read     =   new   streamreader(application.startuppath+ "\\filelist.txt ");
string   strs= " ";
statusbarpanel   p     =   new   statusbarpanel();
statusbarpanel   p1   =   new   statusbarpanel();
//
//statusbar1
//
p.text   = "哈尔滨智科计算机应用技术开发有限公司 ";
p.width   =   250;
p1.width   =   150;
statusbar1.panels.add(p);  
statusbar1.panels.add(p1);  
statusbar1.showpanels   =   true;
timer1.enabled   =true;  
//
//初始化本地文件列表。
//
while((strs=read.readline())!=null)
{
char[]   ch   =   new   char[1]{ ', '};
string[]   strname   =   strs.split(ch);  
filelist1.add(strname[0]);
filelist1.add(strname[1]);  

}
read.close();
progressbar1.visible   =   false;
//读取服务器列表。
myini   ini   =   new   myini(application.startuppath   + "\\serverlist.ini ");
//
//初始化listview1
//
columnheader   biaoti1   =   new   columnheader();
columnheader   biaoti2   =   new   columnheader();
biaoti1.text   = "主机地址 ";
biaoti1.width   =   150;
biaoti2.text   = "主机状态 ";
biaoti2.width   =   150;
listview1.columns.add(biaoti1);
listview1.columns.add(biaoti2);
for(int   i=1;i <=20;i++)
{
string   str   =   ini.getinistring( "serverlist ", "server_ "+i, " ",1024);
if   (str== " ")
{
break;
}
listviewitem   item   =   new   listviewitem();
item.text   =   str;
item.subitems.add( " ");      
listview1.items.add(item);    
label1.text   = "正在测试各服务器状态。 "   ;
}
//读取主程序名称。
windowname   =   ini.getinistring( "windowsname ", "wname ", " ",1024);  
//搜索主程序句柄。
hwnd   =   findwindow(null,windowname);
if(hwnd.equals(intptr.zero))
{
textlog.writetext( "**************************************************************************\r\n ");  
textlog.writetext( "日期: "+datetime.now.tostring()+ "未检测到主程序: "+windowname+   "         程序退出\r\n ");  
textlog.writetext( "**************************************************************************\r\n ");  
application.exit();  
return;
}
//开启线程。
th   =   new   thread(new   threadstart(ceshi));
th.start();  
return;
}
catch(exception   err)
{
textlog.writetext( "日期: "+datetime.now.tostring()+ "   错误信息: "+err.message+ "函数:form1_load\r\n ");          
}
}
private   void   ceshi()
{
try
{
if(listview1.items.count> 0)
{
for(int   i=0;i <listview1.items.count;i++)
{
system.datetime   dtstart   =   system.datetime.now;
string   str   =   listview1.items[i].text;
try
{
iphostentry   hip   =   dns.gethostbyaddress(str);        
}
catch
{
listview1.items[i].subitems[1].text     =   ( "主机没有响应。 ");
continue;  
}
system.datetime   dtend   =   system.datetime.now;
timespan   sp   =   dtend   -   dtstart;
listview1.items[i].subitems[1].text   =   sp.milliseconds.tostring()+ "豪秒 ";
}
hurl= "http:// "+listview1.items[0].text+ "/updata/ ";
label1.text   =   "下载升级文件列表。 ";
groupbox1.text   =     "下载升级文件列表。 ";  
webclient   webc   =   new   webclient();  
string   url   =hurl+ "filelist.txt ";

webc.downloadfile(url, "filelistone.txt ");  
webc.dispose();  
string   str1= " ";

filelist2   =   new   arraylist();  
filesize     =   new   arraylist();  
streamreader   read1   =   new   streamreader(application.startuppath+ "\\filelistone.txt ");  
while((str1=read1.readline())!=null)
{
char[]   ch   =   new   char[1]{ ', '};
string[]   strname   =   str1.split(ch);  
filelist2.add(strname[0]);
filelist2.add(strname[1]);
filesize.add   (strname[2]);
}
read1.close();  
}
thread   =   new   thread(new   threadstart(fenxi));
thread.start();
return;
}
catch(exception   err)
{
textlog.writetext( "日期: "+datetime.now.tostring()+ "   错误信息: "+err.message+ "函数:ceshi\r\n ");          
}

}

发表于:2007-03-17 09:36:514楼 得分:0
private   void   timer1_tick(object   sender,   system.eventargs   e)
{
statusbar1.panels[1].text   =   datetime.now.tostring();  
if(label1.text== "更新完毕。 ")
{
textlog.writetext( "**************************************************************************\r\n ");  
textlog.writetext( "日期: "+datetime.now.tostring()+ "程序退出\r\n ");  
textlog.writetext( "**************************************************************************\r\n ");  
application.exitthread();    
}
}

private   void   fenxi()
{
try
{
label1.text   =   "正在分析文件。 ";
groupbox1.text   =     "正在分析文件。 ";
arraylist   arr   =   new   arraylist();  
arraylist   size   =   new   arraylist();  
for(int   i=0;i <filelist2.count;i+=2)
{
for(int   j=0;j <filelist1.count;j+=2)
{
if(filelist2[i].tostring()==filelist1[j].tostring())
{
if(filelist2[i+1].tostring()!=filelist1[j+1].tostring())
{
arr.add(filelist1[i].tostring());
size.add(filesize[i/2].tostring());      
break;
}
}
}
}
progressbar1.visible   =   true;
if(arr.count==0)
{
label1.text   = "你的程序现在是最新版本,不需要更新。 "   ;
return;
}
updatalist   =   arr;  
for(int   i=0;i <arr.count;i++)
{
int   currdown;
currdown   =   i+1;
label1.text   =   "总共下载文件: "+arr.count.tostring()+ "个         正在下载第 "+currdown.tostring()+ "个文件\n\t "+arr[i].tostring();
webclient   web   =   new   webclient();
stream   stream   =   web.openread(hurl+arr[i].tostring());  
filestream   file   =   new   filestream(application.startuppath+ "\\更新\\ "+arr[i].tostring(),filemode.create);      

long     start   =   0;
progressbar1.maximum   =   convert.toint32(size[i].tostring());    
progressbar1.minimum   =   0;
long   end       =   convert.toint32(size[i].tostring());
long   end1     =   convert.toint32(size[i].tostring());
while(end!=0)
{
byte[]   by   =   new   byte[1024];
int   count   =   stream.read(by,0,1024);
file.write(by,0,count);  
start   +=   count;
progressbar1.value   =   (int)start;
end     -=count;
if(end1 <1000000)
system.threading.thread.sleep(10);        
}
file.flush();
file.close();  
stream.close();  

}

label1.text   =   "下载完毕 ";
groupbox1.text   =     "下载完毕 ";    


if   (!hwnd.equals(intptr.zero))
{
int   i   =   sendmessage(hwnd,wm_close,(intptr)0, " ");
label1.text   = "正在关闭主程序。 ";
thread.sleep(3000);
if(i==0)
{
for(int   j=0;j <updatalist.count;j++)
{
label1.text   = "正在更新文件: "+updatalist[j].tostring();
string   strname   =   application.startuppath+ "\\更新\\ "+updatalist[j].tostring();
file.delete(application.startuppath+ "\\ "+updatalist[j].tostring());      
file.copy(strname,updatalist[j].tostring());
file.delete(strname);
thread.sleep(1000);      
}
}
file.delete(application.startuppath+ "\\filelist.txt ");
file.copy(application.startuppath+ "\\filelistone.txt ", "filelist.txt ");  
system.diagnostics.process.start(application.startuppath+ "\\calc.exe ");
label1.text   = "更新完毕。 ";
}
}

catch(exception   err)
{
textlog.writetext( "日期: "+datetime.now.tostring()+ "   错误信息: "+err.message+ "函数:fenxi\r\n ");      
}
}

private   void   form1_closing(object   sender,   system.componentmodel.canceleventargs   e)
{
textlog.writetext( "**************************************************************************\r\n ");  
textlog.writetext( "日期: "+datetime.now.tostring()+ "程序退出\r\n ");  
textlog.writetext( "**************************************************************************\r\n ");  
thread.abort();
th.abort();  
application.exitthread();    
}


}
}
发表于:2007-03-17 09:43:575楼 得分:25
用application   updater   block生成一个自我更新的winforms   应用(amart   client)  
http://blog.csdn.net/yls087412/archive/2007/01/16/1484233.aspx

下载地址
http://www.microsoft.com/downloads/details.aspx?familyid=c6c09314-e222-4af2-9395-1e0bd7060786&displaylang=en


快速检索

最新资讯
热门点击