新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#WebServices升級程序
面介紹一種用C# Web Services升級程序。通過C# Web Services升級程序就象讀寫本機(jī)文件一樣簡單。所以我就直接給出代碼。

興化網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。創(chuàng)新互聯(lián)自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
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()
- {
- //如果使用設(shè)計的組件,請取消注釋以下行
- //InitializeComponent();
- }
- ///
- /// 需要升級文件的服務(wù)器路徑
- /// summary>
- private const string UpdateServerPath ="d:\\Debug";
- [WebMethod(Description = "返回服務(wù)器上程序的版本號")]
- 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 = "返回給定文件的字節(jié)數(shù)組")]
- public byte[] GetNewFile(string requestFileName)
- {
- ///得到服務(wù)器端的一個文件
- if (requestFileName != null || requestFileName != "")
- return getBinaryFile(UpdateServerPath + "\\"+requestFileName);
- else
- return null;
- }
- ///
- /// 返回所給文件路徑的字節(jié)數(shù)組。
- /// summary>
- /// name="filename"> param>
- ///
returns> - private byte[] getBinaryFile(string filename)
- {
- if (File.Exists(filename))
- {
- try
- {
- //打開現(xiàn)有文件以進(jìn)行讀取。
- FileStream s = File.OpenRead(filename);
- return ConvertStreamToByteBuffer(s);
- }
- catch
- {
- return new byte[0];
- }
- }
- else
- {
- return new byte[0];
- }
- }
- ///
- /// 把給定的文件流轉(zhuǎn)換為二進(jìn)制字節(jié)數(shù)組。
- /// 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();
- }
- }
本文標(biāo)題:C#WebServices升級程序
路徑分享:http://m.5511xx.com/article/djjehci.html


咨詢
建站咨詢
