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不同点
THE END