面介绍一种用C# Web Services升级程序。通过C# Web Services升级程序就象读写本机文件一样简单。所以我就直接给出代码。
兴化网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
C# Web Services升级程序部分代码:
- using System;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.IO;
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class Service : System.Web.Services.WebService
- {
- public Service()
- {
- //如果使用设计的组件,请取消注释以下行
- //InitializeComponent();
- }
- ///
- /// 需要升级文件的服务器路径
- /// summary>
- private const string UpdateServerPath ="d:\\Debug";
- [WebMethod(Description = "返回服务器上程序的版本号")]
- public string ServerVer()
- {
- return "4.0";
- }
- [WebMethod(Description = "返回需更新的文件")]
- public string[] NewFiles()
- {
- DirectoryInfo di = new DirectoryInfo(UpdateServerPath);
- FileInfo[] fi = di.GetFiles();
- int intFiles= fi.Length;
- string[] myNewFiles = new string[intFiles];
- int i = 0;
- foreach (FileInfo fiTemp in fi)
- {
- myNewFiles[i] = fiTemp.Name;
- System.Diagnostics.Debug.WriteLine(fiTemp.Name);
- i++;
- }
- return myNewFiles;
- }
- [WebMethod(Description = "返回需更新的文件的大小")]
- public int AllFileSize()
- {
- int filesize = 0;
- string[] files = Directory.GetFiles(UpdateServerPath);
- foreach (string file in files)
- {
- FileInfo myInfo = new FileInfo(file);
- filesize += (int)myInfo.Length / 1024;
- }
- return filesize;
- }
- [WebMethod(Description = "返回给定文件的字节数组")]
- public byte[] GetNewFile(string requestFileName)
- {
- ///得到服务器端的一个文件
- if (requestFileName != null || requestFileName != "")
- return getBinaryFile(UpdateServerPath + "\\"+requestFileName);
- else
- return null;
- }
- ///
- /// 返回所给文件路径的字节数组。
- /// summary>
- /// name="filename"> param>
- ///
returns> - private byte[] getBinaryFile(string filename)
- {
- if (File.Exists(filename))
- {
- try
- {
- //打开现有文件以进行读取。
- FileStream s = File.OpenRead(filename);
- return ConvertStreamToByteBuffer(s);
- }
- catch
- {
- return new byte[0];
- }
- }
- else
- {
- return new byte[0];
- }
- }
- ///
- /// 把给定的文件流转换为二进制字节数组。
- /// summary>
- /// name="theStream"> param>
- ///
returns> - private byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
- {
- int b1;
- System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
- while ((b1 = theStream.ReadByte()) != -1)
- {
- tempStream.WriteByte(((byte)b1));
- }
- return tempStream.ToArray();
- }
- }
分享文章:C#WebServices升级程序
当前链接:http://www.csdahua.cn/qtweb/news41/30341.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网