spybot-阿宝色图片
2023年4月3日发(作者:野花视频在线观看)
C#WinFrom与WebServer自动下载程序
对于C/S结构程式中自动更新程式是不可缺少部分,但是通过网上查找很难找到相关文档,具
体项目实际情况,写了一个C#WinFrom与WebServer自动下载程序
m自动与webserver版本检查获得更新路径
2.获取更新的filelist
3.下载更新
4.更新后自动运行程式
依上思想我将先成webserver谈起.
一、webserver段
1.Xml文档()
<?xmlversion="1.0"encoding="utf-8"?>
2.XML说明
1、AvailableVersion:serve段的版本(要在更新程式URL中增加相应的文件夹,
把需要更新程式放置与该文件夹下)
2、ApplicationUrl:更新程式Url。
3、ApplicationLoad:文件路径,用于遍历文件所用。
3.C#程式部分
4.usingSystem;
5.c;
6.;
7.;
8.es;
9.;
10.;
11.;
12.;
13.;
14.rs;
15.;
16.//es;
17.ols;
18.
19.
20.namespaceWebService
21.{
22.///
23.///Service1的摘要说明
24.///
25.[WebService(Namespace="/")]
26.[WebServiceBinding(ConformsTo=rofile1_1)]
27.[xItem(false)]
28.//若要允许使用X从脚本中调用此Web服务,请取消对下行的注释。
29.//[Service]
30.publicclassService1:vice
31.{
32.//privatestaticstringstrpath=@"./Update/";
33.privatestaticstringstrpath=@"F:AntoUpdateWebServiceUpdate";
34.//privatestaticstringstrpath=@"http://localhost/WebService/Update";
35.privatestaticInt32File_Count=0;
36.privatestaticfloatFile_Size=0;
37.privatestaticstringSourcepath="";
38.
39.////[WebMethod]
40.////publicstringHelloWorld()
41.////{
42.////return"HelloWorld";
43.////}
44.[WebMethod(Description="取得更新路径")]
45.publicstringCheckForUpdate(stringclientVersion)
46.{
47.stringupdateUrl="";
48.XmlDocumentdoc=newXmlDocument();
49.(h(""));
50.XmlNodenode=SingleNode("/VersionConfig");
51.if(node!=null)
52.{
53.if(node["AvailableVersion"].eTo(clientVersion)>0)
54.{
55.updateUrl=node["ApplicationUrl"].InnerText+
node["AvailableVersion"].InnerText;
56.}
57.}
58.returnupdateUrl;
59.}
60.
61.[WebMethod(Description="取得更新版本")]
62.publicstringGetCurrentVer()
63.{
64.stringcurrentVer="1.0.0.0";
65.XmlDocumentdoc=newXmlDocument();
66.(h(""));
67.XmlNodenode=SingleNode("/VersionConfig");
68.if(node!=null)
69.{
70.currentVer=node["AvailableVersion"].InnerText;
71.}
72.returncurrentVer;
73.}
74.[WebMethod(Description="更新明细")]
75.umentGetUpdateData()
76.{
77.XmlDocumentdoc=newXmlDocument();
78.(h(""));
79.//(h(""));
80.//XmlNodeRoot=SingleNode("VersionConfig");
81.//XmlNodeFListNode=SingleNode("FileList");
82.//XmlNodeFNode=SingleNode("File");
83.//XmlElementXEl=Element("File");
84.//ribute("ID","3");
85.//ribute("Name","");
86.//ribute("Size","");
87.//ribute("Sourcepath","");
88.//Child(XEl);
89.File_Count=0;
90.File_Size=0;
91.WriteXML(doc);
92.
93.returndoc;
94.}
95.privatevoidWriteXML(XmlDocumentdoc)
96.{
97.//XmlNodeRoot=SingleNode("VersionConfig");
98.//XmlNodeFListNode=SingleNode("FileList");
99.//XmlNodeFNode=SingleNode("File");
100.//XmlElementXEl=Element("File");
101.//ribute("ID","3");
102.//ribute("Name","");
103.//ribute("Size","");
104.//ribute("Sourcepath","");
105.//Child(XEl);
106.//获取文件的路径
107.XmlNodenode=SingleNode("/VersionConfig");
108.strpath=node["ApplicationLoad"].InnerText+
node["AvailableVersion"].InnerText;
109.
110.bianli(strpath,doc);
111.//改写文件个数与大小
112.//XmlNodeUpdateNode=SingleNode("FileList");
113.//XmlNodeListUpdateNode=mentsByTagName("FileList");
114.//UpdateNode.
115.//utes("File_Count")=File_Count;
116.XmlNodeListUPdateNode=mentsByTagName("FileList");
117.foreach(XmlNodexduinUPdateNode)
118.{
119.XmlElementxeu=xduasXmlElement;
120.ribute("File_Count",ng(File_Count));
121.ribute("File_Size",ng(File_Size));
122.}
123.
124.
125.}
126.privatevoidbianli(stringpath,XmlDocumentdoc)
127.{
128.
129.string[]filenames=es(path);
130.//F:/实战实例/xml/xml/bin/Debug/Updae/
131.string[]directories=eSystemEntries(path);
132.foreach(stringfileinfilenames)
133.{
134.//Files=
135.//Files[File_Count]=file;
136.FileInfoMyFileInfo=newFileInfo(file);
137.Sourcepath=e(strpath,"").Replace(,
"").Replace("","/");
138.File_Count=File_Count+1;
139.File_Size=File_Size+;
140.XmlNodeRoot=SingleNode("VersionConfig");
141.XmlNodeFListNode=SingleNode("FileList");
142.XmlNodeFNode=SingleNode("File");
143.XmlElementXEl=Element("File");
144.ribute("ID",ng(File_Count));
145.ribute("Name",);
146.ribute("Size",ng());
147.ribute("Sourcepath",Sourcepath);
148.Child(XEl);
149.
150.}
151.foreach(stringDirsindirectories)
152.{
153.stringmFlag="N";
154.foreach(stringmFileinfilenames)
155.{
156.if(mFile==Dirs)
157.{
158.mFlag="Y";
159.}
160.}
161.if(mFlag=="N")
162.{
163.bianli(Dirs,doc);
164.}
165.
166.}
167.
168.}
169.//[WebMethod(Description="取得更新版本")]
170.//publicstringGetVer()//毕业论文
171.//{
172.//XmlDocumentdoc=newXmlDocument();
173.//(h(""));
174.//XmlElementroot=ntElement;
175.//SingleNode("version").InnerText;
176.//}
177.//[WebMethod(Description="在线更新软件")]
178.
179.//[SoapHeader("sHeader")]
180.//umentGetUpdateData()
181.//{
182.////[SoapHeader("sHeader")]
183.////验证用户是否登陆
184.//if(sHeader==null)
185.//returnnull;
186.//
if(!ogin(me,rd))
187.//returnnull;
188.////取得更新的xml模板内容
189.//XmlDocumentdoc=newXmlDocument();
190.//(h(""));
191.//XmlElementroot=ntElement;
192.////看看有几个文件需要更新
193.//XmlNodeupdateNode=SingleNode("filelist");
194.//stringpath=utes["sourcepath"].Value;
195.
196.//intcount=
(utes["count"].Value);
197.////将xml中的value用实际内容替换
198.//for(inti=0;i=odes[i];
199.//stringfileName=path+
utes["name"].Value;
200.//FileStreamfs=
ad(h(fileName));
201.//utes["size"].Value=
ng();
202.//BinaryReaderbr=newBinaryReader(fs);
203.////这里是文件的实际内容,使用了Base64String编码
204.//SingleNode("value").InnerText=
64String(tes((int)),0,(int));
205.//();
206.//();
207.//}
208.//returndoc;
209.//}
210.
211.
212.
213.}
214.}
3.技术难点
1)、就是FileList的获取
二、chile段
1、XML文件配置
<?xmlversion="1.0"encoding="utf-8"?>
Arguments="-1">
2、XML文件配置
1)、AvailableVersion:本地最大版本
2)、DownLoadTo:文件下载到本地路径
3)、AfterRunPrjec:更新后运行程式。PrjectLoad:路径;Arguments:参数
3、Webserver增加
点击引用,选择服务器引用增加
4、C#代码
5、usingSystem;
6、tions;
7、c;
8、entModel;
9、;
10、g;
11、;
12、;
13、;
14、;
15、;
16、;
17、s;
18、;
19、
20、
21、namespacecleint
22、{
23、publicpartialclassPrjUpdate:Form
24、{
25、publicPrjUpdate()
26、{
27、InitializeComponent();
28、}
29、privatestaticstringAvailableVersion="";
30、privatestaticstringDownLoadTo;
31、privatestaticstringAfterRunPrject;
32、privatestaticstringAfterRunPrjectArguments;
33、privatestaticstringSever_AvailableVersion="";
34、privatestaticstringApplicationUrl="";
35、privatestaticInt32File_Tatal=0;
36、privateWebClientFile_client=newWebClient();
37、//publicstaticboolExists(stringpath);
38、privatevoidbutton1_Click(objectsender,EventArgse)
39、{
40、
41、//Update_File();
42、
43、
44、}
45、
46、privatevoidPrjUpdate_Load(objectsender,EventArgse)
47、{
48、//Update_File();
49、Get_AppStartConfig();
50、Update_File();
51、}
52、privatevoidUpdate_File()
53、{
54、File_Tatal=0;
55、try
56、{
57、Client=e1SoapClient();
58、Sever_AvailableVersion=rentVer().ToString();
59、ApplicationUrl=orUpdate(AvailableVersion);
60、stringmFile="";
61、if(ApplicationUrl!="")
62、{
63、if(("有新版本是否需要更新?","新版本更新信息",
el,on)==)
64、{
65、
66、XmlElementServer_EL=(XmlElement)ateData();
67、XmlNodeListFileListNodeList=
Server_Nodes("FileList");
68、XmlNodeListFileNodeList=
Server_mentsByTagName("File");
69、Int32File_Count=0;
70、Int32File_Size=0;
71、foreach(XmlNodeFileListinFileListNodeList)
72、{
73、XmlElementFileList_EL=(XmlElement)FileList;
74、File_Count=
32(FileList_ribute("File_Count"));
75、File_Size=
32(FileList_ribute("File_Size"));
76、}
77、Update_="共用"+ng(File_Count)+"
文件将被更新;文件大小约为为:";
78、Update_=Update_+
ng(File_Size)+"rn";
79、
80、foreach(XmlNodeFileNodeinFileNodeList)
81、{
82、XmlElementFile_El=(XmlElement)FileNode;
83、mFile=File_ribute("Name");
84、Update_=Update_+"正在更新:"+mFile
+"rn";
85、File_Tatal=File_Tatal+
32(File_ribute("Size"));
86、DownloadFile(ApplicationUrl,
File_ribute("Sourcepath"),mFile,DownLoadTo);
87、Update_=Update_+mFile+"更新完毕!"
+"rn";
88、="正在更新程式,共完成:"+
ng((File_Tatal/(File_Size*1.0))*100)+"%";
89、=(File_Tatal/File_Size)*100;
90、
91、}
92、
93、//回写最大版本至本地
94、Update_AppStartConfig("Ver",Sever_AvailableVersion);
95、
96、}
97、
98、}
99、}
100、catch
101、{
102、("更新失败!","Error");
103、}
104、();
105、Run_Prject(AfterRunPrject,AfterRunPrjectArguments);
106、
107、}
108、privatevoidCreate_load(stringmPath)
109、{
110、mPath=e("","/");
111、if(!((mPath)))
112、{
113、Directory(mPath);
114、}
115、//stringmStrPath;
116、//stringDir;
117、//Dir=ing(0,f(":/")+2);
118、//mPath=ing(f(":/")+2,-3);
119、//mStrPath=Dir;
120、//while(>0)
121、//{
122、//mStrPath=mStrPath+ing(0,
f("/")+1);
123、//if(!((mStrPath)))
124、//{
125、//Directory(mStrPath);
126、//}
127、////else
128、////{
129、////Directory(mStrPath);
130、////}
131、//mPath=ing(f("/")+1,-
(f("/")+1));
132、//}
133、
134、
135、}
136、privatevoidDownloadFile(stringmURL,stringmSourcepath,string
mFile,stringmDownLoadTo)
137、{
138、//stringURL=;
139、//intn=dexOf("/");
140、stringURLAddress=mURL+mSourcepath;
141、stringfileName=mFile;
142、stringDir=(mDownLoadTo+mSourcepath).Replace("//","/");
143、stringPath=ng()+fileName;
144、//创建文件夹
145、Create_load(Dir);
146、try
147、{
148、WebRequestmyre=(URLAddress);
149、}
150、catch(WebExceptionexp)
151、{
152、(e,"Error");
153、}
154、try
155、{
156、//="开始下载文件...";
157、File_adFile(URLAddress,fileName);
158、Streamstr=File_ad(URLAddress);
159、StreamReaderreader=newStreamReader(str);
160、byte[]mbyte=newbyte[100000];
161、intallmybyte=(int);
162、intstartmbyte=0;
163、//="正在接收数据...";
164、while(allmybyte>0)
165、{
166、intm=(mbyte,startmbyte,allmybyte);
167、if(m==0)
168、break;
169、startmbyte+=m;
170、allmybyte-=m;
171、}
172、FileStreamfstr=newFileStream(Path,Create,
);
173、(mbyte,0,startmbyte);
174、();
175、();
176、//="下载完毕!";
177、//("下载完毕");
178、}
179、catch(WebExceptionexp)
180、{
181、(e,"Error");
182、//="";
183、}
184、//d=true;
185、//////if(mURL==""||mURL=="http://"||mDownLoadTo=="")
186、//////{
187、//////("请正确输入网址和保存的文件名","提示");
188、//////}
189、//////else
190、//////{
191、//////stringfileName=mDownLoadTo;
192、//////stringurl=mURL+mSourcepath+mFile;
193、//////if(f(@"http://")==-1)
194、//////{
195、//////url=@"http://"+url;
196、//////}
197、////////构造web请求
198、//////HttpWebRequestrequest=
(HttpWebRequest)(url);
199、//////="Get";
200、//////ive=true;
201、//////tType=
"application/x-www-form-urlencoded";
202、////////r="";
203、//////CookieContainercookieCon=newCookieContainer();
204、//////kies(new
Uri(""),
"WebEnv=0hrfhL5jyl0alxtesNazW82DqYlNKl5ZHEz7J1e8eWBYW7CVoTTSAB%40D44C5A7657E83850_0
188SID,ncbi_sid=D44C5A7657E83850_0188SID");
205、//////Container=cookieCon;
206、////////kies(new
Uri(url),cookieheader);
207、////////发送请求,获取响应
208、//////HttpWebResponseresponse=
(HttpWebResponse)ponse();
209、////////使用到的读写流
210、//////StreaminStream=null;
211、//////FileStreamfileStream=null;
212、//////try
213、//////{
214、//////inStream=ponseStream();
215、//////longfileSizeInBytes=tLength;
216、//////fileStream=newFileStream(fileName,
Create,);
217、////////读取数据缓冲区长度和缓冲区
218、//////intlength=1024;
219、//////byte[]buffer=newbyte[1025];
220、//////intbytesread=0;
221、////////从网络读取数据
222、//////while((bytesread=(buffer,0,length))>
0)
223、//////{
224、//////(buffer,0,bytesread);
225、//////}
226、//////("文件创建/写入成功。","提示",
,ation);
227、//////}
228、//////catch(Exceptionfe)
229、//////{
230、//////("错误信息:"+e,"提示",
,);
231、//////}
232、//////finally
233、//////{
234、//////if(inStream!=null)
235、//////{
236、//////();
237、//////}
238、//////if(fileStream!=null)
239、//////{
240、//////();
241、//////}
242、//////}
243、//////}
244、//////////////建立socket实例并建立连接
245、////////////得到保存文件名
246、//////////stringfileName=mDownLoadTo+mSourcepath;
247、////////////得到主机名
248、//////////stringhostName=mURL+mSourcepath+mFile;
249、////////////得到主机信息
250、//////////IPHostEntryipInfo=tByName(hostName);
251、
252、////////////取得IPAddress地址列表
253、//////////IPAddress[]ipAddr=sList;
254、
255、////////////得到ip
256、//////////IPAddressip=ipAddr[0];
257、////////////组合出远程终结点
258、//////////IPEndPointhostEP=newIPEndPoint(ip,80);
259、////////////创建socket实例
260、//////////Socketsocket=newSocket(etwork,
,);
261、
262、//////////try
263、//////////{
264、////////////尝试连接
265、//////////t(hostEP);
266、//////////}
267、//////////catch(Exceptionse)
268、//////////{
269、//////////("连接错误"+e,"提示",
ancel,ation);
270、//////////}
271、
272、//////////////向主机发送请求
273、////////////发送给远程主机的页面请求内容串
274、//////////stringsendStr="GET/HTTP/1.1rnHost:"+
hostName+"rnConnection:Closernrn";
275、////////////创建bytes字节数组以发送串
276、//////////byte[]bytesSendStr=newbyte[1024];
277、////////////将发送内容字符串转换成字节byte数组
278、//////////bytesSendStr=
es(sendStr);
279、//////////try
280、//////////{
281、////////////向主机发送请求
282、//////////(bytesSendStr,,
0);
283、//////////}
284、//////////catch(Exceptionce)
285、//////////{
286、//////////("发送错误:"+e,"提示
",ancel,ation);
287、//////////}
288、//////////////接收数据
289、////////////声明接收返回内容的字符串
290、//////////stringreceiveStr="";
291、////////////声明字节数组,一次接收数据的长度为1024字节
292、//////////byte[]receiveBytes=newbyte[1024];
293、////////////返回实际接收内容的字节数
294、//////////intbytes=0;
295、////////////循环读取,直到接收完所有数据
296、//////////while(true)
297、//////////{
298、//////////bytes=e(receiveBytes,
,0);
299、////////////读取完成后退出循环
300、//////////if(bytes<=0)
301、//////////break;
302、////////////将读取的字节数转换成字符串
303、//////////receiveStr+=
ing(receiveBytes,0,bytes);
304、//////////}
305、////////////将读取的字符串转换成字节数组
306、//////////strings=
ing(f("<"));
307、//////////byte[]content=
es(s);
308、//////////////保存文件
309、//////////try
310、//////////{
311、////////////创建文件流对象实例
312、//////////FileStreamfs=newFileStream(fileName,
Create,ite);
313、
314、////////////写入文件
315、//////////(content,0,);
316、////////////保存成功提示
317、//////////("文件创建/写入成功。","提示",
,ation);
318、//////////}
319、//////////catch(Exceptionfe)
320、//////////{
321、//////////("文件创建/写入错误:n"+
e,"提示",,g);
322、//////////}
323、////////////禁用Socket
324、//////////wn();
325、////////////关闭Socket
326、//////////();
327、//////////}
328、
329、}
330、
331、privatevoidtextBox2_TextChanged(objectsender,EventArgse)
332、{
333、
334、}
335、privatevoidGet_AppStartConfig()
336、{
337、XmlDocumentAppXmlDoc=newXmlDocument();
338、(@"");
339、XmlNodeVerConfigNode=
SingleNode("VersionConfig");
340、if(VerConfigNode!=null)
341、{
342、AvailableVersion=VerConfigNode["AvailableVersion"].InnerText;
343、DownLoadTo=VerConfigNode["DownLoadTo"].InnerText;
344、XmlNodeAfterRunPrjectNode=
SingleNode("AfterRunPrject");
345、XmlElementAfterRunPrject_EL=(XmlElement)AfterRunPrjectNode;
346、AfterRunPrject=AfterRunPrject_ribute("PrjectLoad");
347、AfterRunPrjectArguments=
AfterRunPrject_ribute("Arguments");
348、//AfterRunPrject=VerConfigNode["11"].InnerText;
349、}
350、
351、}
352、privatevoidUpdate_AppStartConfig(stringUpdate_type,stringSver)
353、{
354、XmlDocumentAppXmlDoc=newXmlDocument();
355、(@"");
356、XmlNodeVerConfigNode=
SingleNode("VersionConfig");
357、if(VerConfigNode!=null)
358、{
359、if(Update_type=="Ver"||Update_type=="*")
360、{
361、VerConfigNode["AvailableVersion"].InnerText=Sver;
362、}
363、
364、("");
365、}
366、
367、}
368、privatevoidRun_Prject(stringPrjectLoad,stringPrjectArguments)
369、{
370、PrjectLoad=e("","/");
371、sStartInfoinfo=new
sStartInfo();
372、nts=PrjectArguments;//
ing(f("-")+1,-
f("/")-1);//"";
373、me=
ing(dexOf("/")+1,-
dexOf("/")-1);//"";
374、gDirectory=
ing(0,dexOf("/"));
375、
376、spro;
377、try
378、{
379、pro=(info);
380、}
381、catch(32Exceptionex)
382、{
383、ine("系统找不到指定的文件。r{0}",ng());
384、return;
385、}
386、ine("外部程序启动时间:{0}",ime);
387、rExit(30000);
388、if(ted==false)
389、{
390、ine("有主程序强行终止外部程序运行!");
391、();
392、}
393、else
394、{
395、ine("由程序正常退出");
396、ine("外部程序结束运行时间为:{0}",me);
397、ine("外部程序结束返回值为:{0}",de);
398、}
399、}
400、
401、
402、}
403、}
4、技术难点
1.要在文件中增加e1SoapClient
Client;
2.获取webserver更新File文件。
的ISS要设定为匿名登录,和脚本或者执行文件
4.
ver服务器调正。更改文件中
address="http://172.21.22.16/WebService/即可。
6.
更多推荐
updateservice
发布评论