C#WebServices升级程序

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

兴化网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

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

分享文章:C#WebServices升级程序
当前链接:http://www.csdahua.cn/qtweb/news41/30341.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网