C# Web Services升级程序

开发 后端
本文介绍一种用C# Web Services升级程序。通过C# Web Services升级程序就象读写本机文件一样简单。所以我就直接给出代码。

面介绍一种用C# Web Services升级程序。通过C# Web Services升级程序就象读写本机文件一样简单。所以我就直接给出代码。

C# Web Services升级程序部分代码:

  1. using System;  
  2. using System.Web;  
  3. using System.Web.Services;  
  4. using System.Web.Services.Protocols;  
  5. using System.IO;  
  6.  
  7. [WebService(Namespace = "http://tempuri.org/")]  
  8. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  9. public class Service : System.Web.Services.WebService  
  10. {  
  11. public Service()  
  12. {  
  13. //如果使用设计的组件,请取消注释以下行  
  14. //InitializeComponent();  
  15. }  
  16. /// <summary> 
  17. /// 需要升级文件的服务器路径  
  18. /// summary> 
  19. private const string UpdateServerPath ="d:\\Debug";  
  20. [WebMethod(Description = "返回服务器上程序的版本号")]  
  21. public string ServerVer()  
  22. {  
  23. return "4.0";  
  24. }  
  25. [WebMethod(Description = "返回需更新的文件")]  
  26. public string[] NewFiles()  
  27. {  
  28. DirectoryInfo di = new DirectoryInfo(UpdateServerPath);  
  29. FileInfo[] fi = di.GetFiles();  
  30. int intFilesfi.Length;  
  31. string[] myNewFiles = new string[intFiles];  
  32. int i = 0;  
  33. foreach (FileInfo fiTemp in fi)  
  34. {  
  35. myNewFiles[i] = fiTemp.Name;  
  36. System.Diagnostics.Debug.WriteLine(fiTemp.Name);  
  37. i++;  
  38. }  
  39.  
  40. return myNewFiles;  
  41. }  
  42. [WebMethod(Description = "返回需更新的文件的大小")]  
  43. public int AllFileSize()  
  44. {  
  45. int filesize = 0;  
  46. string[] files = Directory.GetFiles(UpdateServerPath);  
  47. foreach (string file in files)  
  48. {  
  49. FileInfo myInfo = new FileInfo(file);  
  50. filesize += (int)myInfo.Length / 1024;  
  51. }  
  52. return filesize;  
  53. }  
  54.  
  55. [WebMethod(Description = "返回给定文件的字节数组")]  
  56. public byte[] GetNewFile(string requestFileName)  
  57. {  
  58. ///得到服务器端的一个文件  
  59. if (requestFileName != null || requestFileName != "")  
  60. return getBinaryFile(UpdateServerPath + "\\"+requestFileName);  
  61. else  
  62. return null;  
  63. }  
  64.  
  65. /// <summary> 
  66. /// 返回所给文件路径的字节数组。  
  67. /// summary> 
  68. /// <param name="filename">param> 
  69. /// <returns>returns> 
  70. private byte[] getBinaryFile(string filename)  
  71. {  
  72. if (File.Exists(filename))  
  73. {  
  74. try  
  75. {  
  76. //打开现有文件以进行读取。  
  77. FileStream s = File.OpenRead(filename);  
  78. return ConvertStreamToByteBuffer(s);  
  79. }  
  80. catch  
  81. {  
  82. return new byte[0];  
  83. }  
  84. }  
  85. else  
  86. {  
  87. return new byte[0];  
  88. }  
  89. }  
  90. /// <summary> 
  91. /// 把给定的文件流转换为二进制字节数组。  
  92. /// summary> 
  93. /// <param name="theStream">param> 
  94. /// <returns>returns> 
  95. private byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)  
  96. {  
  97. int b1;  
  98. System.IO.MemoryStream tempStream = new System.IO.MemoryStream();  
  99. while ((b1 = theStream.ReadByte()) != -1)  
  100. {  
  101. tempStream.WriteByte(((byte)b1));  
  102. }  
  103. return tempStream.ToArray();  
  104. }  
  105.  

【编辑推荐】

  1. 定义C#接口学习经验
  2. C# ListBox学习笔记
  3. 操作C# Dataset介绍
  4. C# ODBC访问MySQL数据库
  5. 浅析C#和Java不同点
责任编辑:佚名 来源: IT168
相关推荐

2009-08-24 17:34:14

C#创建XML Web

2009-01-19 11:07:42

C#Web.NET

2009-09-03 17:36:13

C#创建Web应用程序

2009-07-31 17:14:19

C#语言Web程序

2009-07-29 17:42:47

ibmdwWeb2.0

2009-08-27 11:53:45

ibmdw云计算

2009-08-13 17:04:09

C#语言C#程序

2009-08-20 15:38:50

C#建立Web Ser

2009-08-11 13:27:22

C#创建Web Ser

2009-09-04 17:58:38

C# Web Brow

2009-08-19 17:11:49

C#程序集

2009-08-17 17:16:19

C#实现在线升级

2009-08-20 17:49:53

学习C#程序

2013-11-11 11:10:41

Amazon Web AWS云服务

2009-08-07 17:32:17

C#编译程序

2009-01-03 14:32:20

ibmdwWebServicesEclipse

2013-11-18 09:49:00

亚马逊Web ServiceCloudTrail

2009-08-27 10:52:36

ibmdw云计算

2009-07-24 16:05:05

调用Web ServiASP.NET

2009-08-24 15:46:46

C# SmartPho
点赞
收藏

51CTO技术栈公众号