盛大錦書註冊到雲梯的 C# 程式

來源:互聯網
上載者:User

我參加了盛大組織的“Bambook程式達人賽”,是通過部落格園報名的。目前提交了兩件參賽作品。

在參賽作品中需要實現“註冊到雲梯”的功能,如所示:

 

這是一個通用的功能,可以封裝為一個類,以便各個參賽的 C# 程式調用。

根據盛大官方的 SDK 文檔,要註冊應用程式到雲梯,有兩種方法:

  1. 使用 RegApp.exe。
  2. 編輯雲梯安裝目錄下的 bbapps.xml 檔案。

我決定採用第二種方法,即編輯雲梯安裝目錄下的 bbapps.xml 檔案。

之所以不採用第一種方法,其中一個原因是第一種方法需要隨程式一起發布 RegApp.exe。

 

好了,下面就是核心的 BambookAppsXml.cs 來源程式檔案:

01:  using System;02:  using System.Xml;03:  using System.IO;04:  using Microsoft.Win32;05:  06:  namespace Skyiv.Common07:  {08:    /// <summary>09:    /// 盛大雲梯軟體的應用程式擴充的設定檔10:    /// </summary>11:    public sealed class BambookAppsXml12:    {13:      static readonly string XmlFileName = "bbapps.xml";14:      static readonly string ElementApp = "app";15:      static readonly string ElementId = "id";16:      static readonly string ElementExec = "exec";17:  18:      XmlDocument doc;19:  20:      /// <summary>21:      /// 註冊應用程式到雲梯22:      /// </summary>23:      /// <param name="guid">應用程式識別碼</param>24:      /// <param name="executeFileFullName">應用程式全路徑名</param>25:      public void InsertOrUpdate(Guid guid, string executeFileFullName)26:      {27:        if (guid == Guid.Empty) throw new Exception("程式標識不可為空");28:        var path = GetCloudLibraryInstallPath();29:        if (path == null) throw new Exception("雲梯軟體尚未安裝,請先下載並安裝雲梯。");30:        var fileName = Path.Combine(path, XmlFileName);31:        doc = new XmlDocument();32:        doc.Load(fileName);33:        var id = guid.ToString("N");34:        var elem = FindElementId(id);35:        if (elem == null) doc.DocumentElement.AppendChild(elem = CreateElementId(id));36:        InsertOrUpdateElementExec(elem, executeFileFullName);37:        doc.Save(fileName);38:      }39:  40:      XmlElement FindElementId(string id)41:      {42:        foreach (var node in doc.DocumentElement.ChildNodes)43:        {44:          var elem = node as XmlElement;45:          if (elem == null) continue;46:          foreach (var node2 in elem.ChildNodes)47:          {48:            var elem2 = node2 as XmlElement;49:            if (elem2 == null) continue;50:            if (elem2.Name != ElementId) continue;51:            if (elem2.InnerText == id) return elem;52:          }53:        }54:        return null;55:      }56:  57:      XmlElement CreateElementId(string id)58:      {59:        var elem = doc.CreateElement(ElementApp);60:        var elemId = doc.CreateElement(ElementId);61:        elemId.InnerText = id;62:        elem.AppendChild(elemId);63:        return elem;64:      }65:  66:      void InsertOrUpdateElementExec(XmlElement elem, string value)67:      {68:        foreach (var node2 in elem.ChildNodes)69:        {70:          var elem2 = node2 as XmlElement;71:          if (elem2 == null) continue;72:          if (elem2.Name != ElementExec) continue;73:          elem2.InnerText = value;74:          return;75:        }76:        var elemExec = doc.CreateElement(ElementExec);77:        elemExec.InnerText = value;78:        elem.AppendChild(elemExec);79:      }80:  81:      /// <summary>82:      /// 擷取雲梯軟體的安裝目錄83:      /// </summary>84:      /// <returns>雲梯軟體的安裝目錄</returns>85:      string GetCloudLibraryInstallPath()86:      {87:        return Path.GetDirectoryName(Registry.GetValue(88:          @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Bambook",89:          "AutoRunExec", null) as string);90:      }91:    }92:  }

這個 BambookAppsXml 類的主要功能是通過編輯盛大雲梯軟體安裝目錄下的 bbapps.xml 檔案來將應用程式註冊到雲梯。

 

上述程式中第 85 到第 95 行的 GetCloudLibraryInstallPath 方法通過讀取註冊表的以下索引值:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Bambook\AutoRunExec

來獲得雲梯軟體的安裝目錄。

 

雲梯軟體安裝目錄下的 bbapps.xml 檔案格式如下所示:

01:  <bbapps>02:    <app>03:      <id>1aba95ad66ec70263a79fa260394e170</id>04:      <exec>E:\Bambook\Air\TVGuide\TVGuide.exe</exec>05:    </app>06:    <app>07:      <id>88a915cc3658c7130910da4e312436f3</id>08:      <exec>E:\work\PowerWord2Snb\PowerWord2Snb.exe</exec>09:    </app>10:  </bbapps>

然後上述程式就通過 System.Xml 命名空間的下的 XmlDocument 類以及相關的類來編輯這個 XML 檔案,從而達到將應用程式註冊到雲梯的目的。

 

在 AboutDiaglog 類的“註冊到雲梯”按鈕的事件處理常式中調用 BambookAppsXml 類的 InsertOrUpdate 方法來註冊應用程式到雲梯,如下所示:

01:  private void btnRegisterApp_Click(object sender, EventArgs e)02:  {03:    try04:    {05:      new BambookAppsXml().InsertOrUpdate(assembly.Guid, assembly.ExecuteFileFullName);06:      MessageBox.Show("完成", "註冊到雲梯", MessageBoxButtons.OK, MessageBoxIcon.Information);07:    }08:    catch (Exception ex)09:    {10:      MessageBox.Show(ex.Message, "註冊到雲梯", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);11:    }12:  }

這個 AboutDialog 類的其餘部分的來源程式代碼請參見 通用的“關於本軟體”對話方塊 一文。

 

最後,就是“錦書背單詞”這個應用程式的 AssemblyInfo.cs 來源程式檔案了:

01:  using System.Reflection;02:  using System.Runtime.InteropServices;03:  04:  [assembly: AssemblyTitle("錦書背單詞")]05:  [assembly: AssemblyDescription("將金山詞霸匯出的生詞本,製作成snb格式的電子書。")]06:  [assembly: AssemblyCompany("Skyiv Studio")]07:  [assembly: AssemblyProduct("PowerWord2Snb")]08:  [assembly: AssemblyCopyright("Copyright   2010")]09:  10:  // 注意,這不是 Visual Studio 2010 自動產生的 Guid。11:  // 這是從盛大官網獲得的 Guid,用於註冊到雲梯。12:  [assembly: Guid("88a915cc-3658-c713-0910-da4e312436f3")]13:  14:  [assembly: AssemblyVersion("1.4")]

這裡要注意的是 GuidAttribute 不能使用 Visual Studio 2010 自動產生的 Guid,要從盛大官網擷取。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.